Build a Merkle Root and Verify a Transaction Inclusion Proof

Last updated: November 29, 2026

Quick Overview

Given the transaction hashes of a block, compute its Merkle root using double SHA-256 and the Bitcoin rule of duplicating the last node on odd levels, then generate and verify an inclusion proof for one transaction.

Coinbase
Coding & Algorithms
Software Engineer
Coinbase
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given the transaction hashes of a block, compute its Merkle root using double SHA-256 and the Bitcoin rule of duplicating the last node on odd levels, then generate and verify an inclusion proof for one transaction.

Coinbase engineers work directly with chain data, and light-client style verification shows up in deposit and custody tooling. The interviewer checks a clean bottom-up tree build, correct handling of odd levels and the left or right ordering of siblings in the proof.

What the Interviewer Expects
  • Build the tree level by level rather than with explicit nodes
  • Duplicate the last hash when a level has odd length
  • Produce a proof of sibling hashes with their side
  • Verify by hashing upward in the right order
  • State O(n) build and O(log n) proof size
Key Topics to Cover
binary trees
hashing
merkle trees
proofs
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What is the CVE-2012-2459 duplicate-leaf issue and how do you guard against it?
  • How would you prove that a transaction is not in a block?
  • How do Ethereum's Merkle Patricia tries differ from this?
  • How would you update the root when one leaf changes?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: a non-empty list of 32-byte leaf hashes. merkle_root returns the root; merkle_proof(leaves, i) returns the sibling path for leaf i; verify(leaf, proof, root) checks it. Parent hash is `sh...

High-Level Design

The tree is complete apart from the last node per level, so an array per level is enough. At each level, pad an odd list by repeating its last element, then pair neighbors. For a proof, the sibling of...


Submit Your Answer
Markdown supported