CursorMerkle Tree over a Repository: Hash & Diff
Build a Merkle tree over the provided repo, where folders are N-ary nodes and files are leaves (file hash = given function over contents; folder hash = hash of concatenated child hashes). Implement get_hash, locate changed files after edits, and diff two trees into (path, ADDED/MODIFIED/REMOVED). Some versions add a follow-up on optimizing client/server sync. 45–60 minutes, with your own tests.
Problem
Implement a Merkle tree (also called a hash tree) over a real directory. The round runs inside a provided repository, so the code has to read actual files from disk, and the folder structure is the tree: every directory is an internal node with any number of children, and every file is a leaf. It is not a binary tree.
The interviewer supplies the hashing rule:
- A text file's hash is a given function applied to its contents.
- A folder's hash is the hash of the concatenated hashes of its children.
The work comes in parts:
- Build the tree for the whole repo and implement
get_hashfor any path (the root hash covers the entire repo). - Modify a file, then use the tree to find which file changed.
- Extend the lookup: when a folder has changed, find every changed file under it. In the most common framing this becomes a full
diffbetween two trees.
You write your own tests and run them against real files before time runs out (45–60 minutes).
One candidate's interface, agreed with the interviewer before coding:
from enum import Enum, auto
from typing import Self
class ChangeType(Enum):
ADDED = auto()
MODIFIED = auto()
REMOVED = auto()
class MerkleTree:
def __init__(self, root_directory: str):
pass
# List of changed files and change types
def diff(self, other: Self) -> list[tuple[str, ChangeType]]:
passVariants
Free-function API
class MerkleTree:
def build(self, path): ...
def get_hash(path, tree): ...
def diff(tree, tree): ...The minimal version asks only for get_hash(path: str) on a MerkleTree built from the provided repo.
Client / server sync follow-up
In one 45-minute version, part 2 asks you to use the hash tree to make client/server update transfers cheaper, instead of locating changed files locally.
Tuple-return API
A second shape of diff returns three lists of file paths, (added, modified, removed), instead of (path, ChangeType) pairs. Both are the same comparison; confirm which output the interviewer wants before coding.
Hints
Pitfalls
- Filesystem fluency decides the round. Know the standard-library file APIs; candidates without Python filesystem experience struggle to finish.
- It is not your practice binary tree. A folder-shaped tree differs from the textbook version in many details. Discussing tree balance is wasted time.
- Shape the diff output early. Model results as added / modified / removed per path from the start. One candidate skipped this and never finished a correct comparison.
- Pin down the interface even after verbal agreement. Spell out signatures and return types in code before writing logic.
- The bar is a clean, complete run. 60 minutes is tight; one candidate finished, fixed a couple of test bugs, and was still rejected.
- AI rules. In one tech screen, looking up syntax with Google or AI was allowed, but using AI to write the implementation was not.
- Type changes. A path that turns from a file into a directory (or back) is easy to miss; report it as a removal plus an addition.
- Path building. Joining with an empty root path gives leading or doubled separators; test root and nested paths.
Talking Points
- Hash pruning is the point: equal subtree hashes let you skip whole directories; without it this is just a file walker.
- Sort children before hashing for determinism; stream files in chunks for large inputs.
- Handle file/directory type changes explicitly, as a removal plus an addition.
- State diff cost as changed subtrees times depth, not full node count.
Follow-ups
Preparation
- Build this end-to-end in Python against a real nested directory (
os/pathlib, reading file contents) and time yourself to under 45 minutes. - Implement
diffthat returns(path, ChangeType)pairs, and write tests for an added file, a removed file, a modified file, and a change nested several folders deep. - Practice writing the full interface (enum plus method signatures) in the first five minutes, before any logic.
