Code RoomMerkle root
MediumPrep Room Coding #1532

Merkle root

CodingStorage & CDNAlgorithms & data structuresMid–Senior~28 min

Compute the Merkle root of a list of integer leaves. Use the deterministic hash combine(a, b) = (a * 31 + b * 17 + 1) % 1000000007 for an internal node from its left child a and right child b; a leaf's hash is its own value taken mod 1000000007. Build the tree level by level: pair up adjacent nodes left to right; if a level has an odd number of nodes, the last (unpaired) node is promoted unchanged to the next level. Repeat until one node remains — that is the root. For an empty leaf list, return 0. Return the root as an integer.

Implement
merkle_root(leaves: list[int]) → int
Examples
in[[1,2]]out66
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 28 min
InputExpectedGot
[[1,2]]66not run yetsample