The Merkle Tree Screen: Hash, Diff & Sync
- 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.
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 nameroot 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.
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 nodedef 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"]]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.
Merkle Tree over a Directory
Problem
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.- MerkleTree({"a.txt": "hello"})
- get_hash("a.txt")→ "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
- get_hash("b.txt")
A file hashes its contents, so a.txt gets the SHA-256 of "hello". Nothing lives at b.txt.
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.
Merkle Tree over a Directory
Click "Run" to execute your code
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.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.Diff Two Merkle Trees
Problem
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.README.md matches at the root, so only src is opened. Inside it, util.py matches and is skipped.
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.
Diff Two Merkle Trees
Click "Run" to execute your code
Update and Sync a Cached Merkle Tree
Problem
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.- 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"})
- edit("src/app.py", "print(2)")→ 3
- in_sync()→ false
- sync()→ ["UPLOAD src/app.py"]
- compared()→ 5
- 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.
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.
Update and Sync a Cached Merkle Tree
Click "Run" to execute your code
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.
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 guideKnow what Cursor asks before you walk in.
9 coding · 4 system design · 16 behavioral questions
Get their questions first.
InterviewLegend is independent and not affiliated with or endorsed by any company it covers.