Design a thread-safe tree locking system
PremiumMachine codingHard
- Tree locking
- Race conditions
- Linearizability
Quick answer
This machine coding interview question asks you to design a thread-safe tree locking system. Aim for a design that meets its requirements at the stated scale, then explain its main components, data flow and trade-offs. Interviewers want to see how you scope the problem, size it and defend each choice.
The problem
Build a thread-safe tree locking system: a library that locks nodes of an m-ary tree for users, where a node cannot be locked while any ancestor or descendant is locked. It is the idea behind hierarchical locks on folders and files. The hard part is making lock, unlock and upgrade fast and still correct when many threads call them at once.
Write working code in any language with threads, mutexes and atomics. Part A is single-threaded and correct; part B makes it fast and thread-safe.
What must a thread-safe tree locking system do?
- Lock: lock(node, user) returns true and locks the node only if the node, all its ancestors and all its descendants are unlocked.
- Unlock: unlock(node, user) returns true only if the node is locked by that same user, and then unlocks it.
How well must it do it?
- Fast operations: lock and unlock cost O(depth), not O(subtree size); upgrade costs O(locked descendants x depth).
- Thread safety: Under 16 threads, no two calls can both succeed when one forbids the other, and counters never drift.
What scale should you design for?
Use these numbers in your estimates.
The scale numbers unlock with this questionUnlock
Key concepts
- Tree locking
- Race conditions
- Linearizability
- Atomic counters
- Deadlock avoidance
- M ary tree
How you’ll practise
- Requirements
- Entities and interfaces
- Core implementation
- Edge cases and tests
- Extensions
- Wrap-up
Lala, personalized to you, gives a hint when you ask and reviews your design when you are ready.
What do interviewers look for?
In any machine coding design round.
- Clarifies the requirements before drawing anything
- Uses a few numbers that actually change the design
- Draws a clear path from the client to storage
- Picks storage and caching with reasons, not by habit
- Explains what breaks first under load and how to fix it
- States trade-offs openly instead of claiming a perfect design
Questions people ask about this interview question
- Which design type is this question?
- It is a machine coding question. You design a thread-safe tree locking system and explain your choices the way you would in an interview.
- Is this question asked at entry level?
- It is marked Hard. It suits engineers preparing for senior interviews, where you are expected to drive the discussion and go deep on trade-offs.
- What should I cover first?
- Start with the requirements: what the system must do, how well it must do it and which numbers change the design. Agree on scope with the interviewer before you design anything.
- Can I practise it for free?
- It is a premium question. Sign up and unlock it once for 10 credits; it then stays unlocked. When you finish, Lala, your personalized AI mentor, can review your design for 30 credits.
Related questions
Updated · Written by the SevyDevy team