InterviewLegend

The Merkle Tree Screen: Hash, Diff & Sync

Alex Chen40 min readGuide updated
In this lesson
  • Build a Merkle tree over a directory and answer get_hash for any path
  • Diff two trees into added, modified and removed files, skipping equal subtrees
  • Update one file in a cached tree and sync only the changed branches
  • Write your own tests for all three inside a 60-minute screen

If you have a Cursor coding screen booked, expect a Merkle tree. It's the most reported Cursor problem, with about a dozen reports from 2025 through September 2026, and it's usually the first screen. You build a tree over a set of files. Each file hashes its contents, each folder hashes its children, and then you use those hashes to find what changed.

Candidates agree on the shape. In September 2026 one candidate had 60 minutes to build the tree, answer get_hash for a path and diff two trees, and had to write every test themselves. They finished with a bug or two and were still turned down. Another September candidate got get_hash for the whole repo first, then a second part that changes a file and asks you to find it. An August 2026 screen ran 45 minutes and opened with a hash tree over a file system.

Earlier in the year it was the same question. A senior ML engineer loop in July 2026 opened with a Merkle tree for a file diff. In February 2026 a candidate worked inside a real repo, reading files with the standard library, and ran out of time on the compare step. Older reports match too. A new-grad round in November 2025 asked for a hash tree like the one Cursor uses itself, in 60 minutes, and a 2025 screen paired the tree with client-server tasks.

The Cursor angle
You're building a small copy of real Cursor code. Cursor's January 2026 post on indexing large codebases describes "a cryptographic hash of every file, along with hashes of each folder that are based on the hashes of its children." Sync compares those hashes with the server's tree and walks only the branches that differ.

What the Cursor Merkle tree question asks

The round runs something like three steps. First you build the tree from a directory and answer get_hash(path) for any file or folder. The folder layout is the tree, so a node can have any number of children. Then you diff two trees and return each changed path with a label like added, modified or removed, opening a folder only when its hashes differ.

Then come the follow-ups. Candidates describe a client and server that sync only the subtrees that changed, which is what Cursor's own indexer does. Versions from 2026 add two more: update a cached tree after one file changes, and say what byte-exact equality would cost compared with trusting the hash. An hour goes fast. The February candidate never got past the diff.

A folder's hash depends only on its children's hashes, so two folders with the same hash hold the same files all the way down. That one property gives you all three parts. The diff skips any subtree whose hashes match, an edit rehashes only the path from the file to the root, and a sync sends only the branches where the two roots disagree.

Pin the contract before you type

The prompt leaves several choices to you, and each one changes the hashes. Decide them in the first few minutes and write them in a comment at the top of the file. Sort a folder's children by name, because directory listings come back in no guaranteed order and an unsorted hash can change between two runs on the same files. Put each child's name in the folder's hash, or a rename leaves every hash above it untouched.

Then settle the edges. Pick a hash function (Cursor's post uses SHA-256). Decide what a path that is a file on one side and a folder on the other should report, whether empty folders count, and what you do with symlinks. None of these is hard, and saying them out loud early shows the interviewer you've thought about the real file system.

file sha256(contents)
folder sha256("README.md <hash of README.md>\n" +
"src <hash of src>\n")
one line per child, children sorted by name
root the folder at path ""

Write your own tests inside the hour

In September 2026 a candidate had to write all the tests for this screen inside the same 60 minutes. Plan for it. Once get_hash works, spend about five minutes on a handful of asserts, and add to them after the diff. Small hand-built repos beat a big fixture, because you can work out the expected answer in your head.

Aim each test at one property. Feeding the same files in a different order should give the same root. Editing one file should change its folders and the root and leave its neighbours alone. A missing path returns nothing, and the empty tree has a defined hash. For the diff, cover no change, one change deep in a folder, a removed folder, and a file that turns into a folder.

python
REPO = {"src/app.py": "print(1)", "src/util.py": "x = 2", "README.md": "hi"}
def test_hashes():
tree = MerkleTree(REPO)
shuffled = MerkleTree(dict(reversed(list(REPO.items()))))
assert shuffled.get_hash("") == tree.get_hash("") # input order never matters
edited = MerkleTree({**REPO, "src/app.py": "print(2)"})
assert edited.get_hash("src/util.py") == tree.get_hash("src/util.py")
assert edited.get_hash("src") != tree.get_hash("src") # the change climbs to the root
assert edited.get_hash("README.md") == tree.get_hash("README.md")
assert tree.get_hash("src/app") is None # a prefix is not a node
def test_diff():
assert diff_trees(REPO, REPO) == []
assert diff_trees(REPO, {"README.md": "hi"}) == [
["src/app.py", "REMOVED"], ["src/util.py", "REMOVED"]]
assert diff_trees({"x": "file"}, {"x/y.txt": "y"}) == [
["x", "REMOVED"], ["x/y.txt", "ADDED"]]
On a real directory, build the fixture in a temp folder. tempfile.TemporaryDirectory() gives you a scratch folder that cleans itself up. Write the same small repo into it with os.makedirs and open, then run the asserts against the disk version. It catches sorting and path-joining bugs that a dict never will.

Asked at Cursor

Three problems, in the order the round tends to run. The real screen walks a directory on disk. Here each repo comes in as a dict from file path to contents so the editor can run it, and the first walkthrough shows the disk version. The hashes in the tests are real SHA-256 values, so your code has to follow the hashing rule exactly.

1

Merkle Tree over a Directory

MediumSignature problem
Problem
Build a MerkleTree over a set of files. The constructor takes files, a dict from each file's path to its contents. Paths use /, and every folder on a path is a node in the tree.

A file's hash is the SHA-256 hex digest of its contents. A folder's hash is the SHA-256 hex digest of one line per child, name + " " + child_hash + "\n", with the children sorted by name. The root is the folder at path "".

get_hash(path) returns the hash of the file or folder at path, or None if nothing is there.
  1. MerkleTree({"a.txt": "hello"})
  2. get_hash("a.txt")→ "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
  3. get_hash("b.txt")

A file hashes its contents, so a.txt gets the SHA-256 of "hello". Nothing lives at b.txt.

How this uses the pattern

Insert each path into nested dicts, one level per folder name. Then hash bottom-up in one post-order pass and store the hash on every node, so get_hash is just a walk down the path.

Write it here, or go full screen with the problem, hints and solution beside the editor.

Merkle Tree over a Directory

Loading...
Click "Run" to execute your code
The Cursor angle
This is part one, and in September 2026 it was the first thing asked: get_hash for any path in the repo. The folder layout is the tree, so a node can have any number of children. Reaching for the binary Merkle tree from a blockchain tutorial sends you the wrong way. Build the folders first, then hash once from the bottom up. Store each hash on its node so a lookup costs a walk down the path. Sorting children by name keeps the hash stable, and so does putting each child's name in its folder's line.
Take it further
Run it on a real directory. In February 2026 the screen ran inside a real repo, with files read through the standard library. A July 2026 version asks about streaming file hashes and a symlink policy too. Walk the folder with os.scandir, sort the entries by name, and feed each file to hashlib.sha256 in chunks so a large file never sits in memory. Skipping symlinks is the safe default, since following one can loop forever. The walkthrough has the disk version.
2

Diff Two Merkle Trees

MediumSignature problem
Problem
Write diff_trees(old_files, new_files). Each argument is a repo snapshot in the first problem's format, a dict from file path to contents. Build a Merkle tree for each one with the same hashing rule.

Return every changed file as a [path, change] pair, sorted by path. A file in both snapshots with different contents is "MODIFIED". A file only in the new snapshot is "ADDED", and a file only in the old one is "REMOVED". When a whole folder appears or disappears, list each file under it.

If a path is a file in one snapshot and a folder in the other, everything on the old side is "REMOVED" and everything on the new side is "ADDED", file by file. Open a folder only when its two hashes differ.
Inputold_files = {"src/app.py": "print(1)", "src/util.py": "x = 2", "README.md": "hi"}new_files = {"src/app.py": "print(2)", "src/util.py": "x = 2", "README.md": "hi", "src/new.py": "n"}Output[["src/app.py", "MODIFIED"], ["src/new.py", "ADDED"]]

README.md matches at the root, so only src is opened. Inside it, util.py matches and is skipped.

How this uses the pattern

Recurse on pairs of nodes. Equal hashes end the branch. Two files with different hashes are MODIFIED. A name present on only one side means every file below it was added or removed.

Write it here, or go full screen with the problem, hints and solution beside the editor.

Diff Two Merkle Trees

Loading...
Click "Run" to execute your code
The Cursor angle
Part two is where the hashes earn their keep. One September 2026 candidate described it as changing a file and asking you to find it, and the February 2026 candidate ran out of time right here. The trap is a diff that compares every file. It passes the tests, but the interviewer asked for a Merkle tree so you would skip equal subtrees. Compare the two root hashes first and return nothing when they match. When they differ, open only the children whose hashes differ, and recurse.
Take it further
Then the second tree moves to a server. Candidates describe a next part where one tree lives on a client and the other on a server, and you sync only the subtrees that changed. The diff you just wrote is most of that. Swap "report the path" for "send the file" or "delete it on the server", and add a counter for how many hashes you compared. The third problem builds it.
3

Update and Sync a Cached Merkle Tree

HardFrequently asked
Problem
Build CodebaseSync, which keeps a client tree and a server tree, both hashed with the first problem's rule. The constructor takes client_files and server_files, two dicts from path to contents.

edit(path, content) writes one client file, making folders as needed. delete(path) removes one and drops any folder left empty. Both update the cached tree in place and return how many hashes they recomputed. For an edit that's the file plus every folder above it, and for a delete it's the folders that remain on the path.

sync() makes the server match the client and returns its actions sorted by path. It returns "UPLOAD path" for each file the server lacks or holds with other contents, and "DELETE path" for each server file the client doesn't have. It compares hashes from the root down and opens a folder only when they differ. The client never changes.

compared() returns how many node pairs the last sync compared, counting the root. in_sync() returns whether the two root hashes match.
  1. CodebaseSync({"src/app.py": "print(1)", "src/util.py": "x = 2", "README.md": "hi"}, {"src/app.py": "print(1)", "src/util.py": "x = 2", "README.md": "hi"})
  2. edit("src/app.py", "print(2)")→ 3
  3. in_sync()→ false
  4. sync()→ ["UPLOAD src/app.py"]
  5. compared()→ 5
  6. in_sync()→ true

The edit rehashes app.py, src and the root. Sync compares the root, README.md, src, app.py and util.py, then uploads the one file that differs.

How this uses the pattern

An edit only touches the path from the file to the root, so rehash those folders from the bottom up and leave the rest. Sync is the diff from the second problem with actions in place of labels.

Write it here, or go full screen with the problem, hints and solution beside the editor.

Update and Sync a Cached Merkle Tree

Loading...
Click "Run" to execute your code
The Cursor angle
This follow-up mirrors Cursor's own indexer. Its January 2026 post says a small edit changes only the edited file's hash and the hashes of the folders above it, and that sync compares hashes with the server "to see exactly where the two Merkle trees diverge." "Entries whose hashes differ get synced," and "the sync process never modifies files on the client side." Candidates place this sync in a second part, and a 2025 screen paired the tree with client-server tasks.
Take it further
Byte-exact, or trust the hash? A 2026 version of the problem asks what extra work byte-exact equality would take. With SHA-256, two different files sharing a hash is not a practical worry, which is why sync can skip any matching subtree. Byte-exact means comparing the bytes of every file even when hashes match, so the pruning is gone and you touch the whole repo again. Say that trade out loud. The walkthrough shows the change and when a shorter hash makes it worth paying.

What earns points

The bar on this screen is high, as the September 2026 rejection above shows. On a different Cursor screen in January 2026, a candidate who passed put it plainly: "Goal from the interviewer was to get a working solution." So aim for correct, tested code that grows into the next part without a rewrite. Clever code you have to tear up at part two costs you the hour.

Talk through the contract, then keep the code small. State your hashing choices out loud, store a hash on every node, and put the equal-hash check at the top of the diff. Run your own tests after each part. When the sync part arrives, reuse the diff and the edit code instead of writing a third tree walk.

Common questions

What is the Cursor Merkle tree interview question?

Something like this: build a tree over a repo or directory where each file hashes its contents and each folder hashes its children, answer get_hash for any path, then diff two trees into added, modified and removed files. Follow-ups cover syncing a client and server, updating a cached tree after one edit, and byte-exact equality. It's the most reported Cursor coding question, with about a dozen reports from 2025 through September 2026.

Can I use AI in the Cursor coding interview?

Not to write the solution. Cursor's CEO said in June 2025 that first technical screens run without AI "other than autocomplete," and 2026 screens allow web search for syntax at most. The coding overview has the exact rules.

Which language should I use?

Whichever you're fastest in, since the screen starts from a template in your language. Most Merkle tree accounts used Python, whose hashlib and os modules cover everything the problem needs.

Is this on LeetCode?

There's no exact match. The nearest relatives are Find Duplicate Subtrees, which hashes subtrees, and Find Duplicate File in System, which groups files by content. Neither asks for a diff or a sync, so practice the three problems above from a blank file instead.

Key Takeaways

  • ✓The Merkle tree is the most reported Cursor problem, about a dozen reports through September 2026, and it usually comes first.
  • ✓Sort children by name and include names in folder hashes, or the hashes drift between runs.
  • ✓Equal hashes mean equal subtrees. The diff and the sync open only the folders where hashes differ.
  • ✓Budget time to write your own tests. A September 2026 candidate had to, inside the 60 minutes.
Cursor interview guide

Know what Cursor asks before you walk in.

9 coding · 4 system design · 16 behavioral questions

Get Premium$49 a year · Instant access
Or start freeNo card.

Get their questions first.

The Cursor Coding InterviewHad your Cursor interview?How we research

InterviewLegend is independent and not affiliated with or endorsed by any company it covers.