← Questions
Lock, unlock and upgrade nodes in a tree
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked Aug 2025. It tests trees, dfs and design.
A rooted tree of N nodes labeled 0..N-1 is given by a parent array: parent[0] = -1 and parent[i] is the parent of node i. Build a lock manager over it. The rules come in two parts; clarify them with the interviewer before coding, because the problem is often given loosely.
Part 1: lock, unlock, is_locked
Unless you agree on a different interface with the interviewer, assume:
LockingTree(parent: list[int])-
lock(x: int) -> bool— locksxonly ifxis unlocked, no ancestor ofxis locked and no descendant ofxis locked. Returns whether it locked. -
unlock(x: int) -> bool— unlocksxif it is locked; returns whether it did. is_locked(x: int) -> bool
parent = [-1, 0, 0, 1, 1, 2]
lock(0) -> True
is_locked(0) -> True
lock(3) -> False # ancestor 0 is locked
unlock(0) -> True
lock(3) -> True
is_locked(3) -> True
parent = [-1, 0, 1, 2, 3] # a chain 0 -> 1 -> 2 -> 3 -> 4
lock(2) -> True
lock(0) -> False # descendant 2 is locked
lock(4) -> False # ancestor 2 is locked
unlock(2) -> True
lock(0) -> True
lock(4) -> False # ancestor 0 is locked
Part 2: locks owned by users, and upgrade
Each lock now belongs to a user.
-
lock(x, user)— same conditions as Part 1; the lock recordsuser. -
unlock(x, user)— succeeds only ifxis locked by thisuser. -
upgrade(x, user) -> bool— succeeds only ifxis unlocked, no ancestor is locked, at least one descendant is locked, and every locked descendant is held byuser. It unlocks all those descendants and locksxforuser.
parent = [-1, 0, 0, 1, 1, 2]
lock(3, "a") -> True
lock(4, "a") -> True
unlock(3, "b") -> False # held by a
upgrade(1, "b") -> False # the locked descendants belong to a
upgrade(1, "a") -> True # 3 and 4 unlocked, 1 locked by a
is_locked(3) -> False
lock(5, "b") -> True
upgrade(0, "b") -> False # descendant 1 is held by a
Constraints
- Up to
2 * 10^5nodes and2 * 10^5operations; everyxis a valid node. - Aim for O(log N) or O(depth) per operation, not a walk of the whole subtree.
Clarifying questions
- Must
lockcheck descendants as well as ancestors? - In
upgrade, must every locked descendant belong to the same user, or any user? - Can the same user lock a node twice? Is unlocking an unlocked node an error or just
False? - How deep can the tree be: is it balanced or can it be a chain?
- Will operations come from several threads at once?
What a strong answer covers
- A
locked_descendantscount per node, updated along the ancestor path on every lock and unlock, so the descendant check is O(1) and each operation is O(depth). - For a guaranteed bound on deep trees: an Euler tour so each subtree is a range, with a Fenwick or segment tree for "any locked in this range", plus a way to check locked ancestors.
-
upgrade: finding the locked descendants without visiting the whole subtree (a set of locked nodes ordered by Euler index), and its cost. - Edge cases: the root, leaves, repeated lock and unlock, conflicting locks, a deep chain.
- Unit tests for each rule, written before or alongside the code.
Follow-ups
- Make it safe when many threads call it at once, without one global lock.
- Locks expire after a timeout.
- Support shared (read) and exclusive (write) locks.
Where it's asked
- Amazon · Technical phone screen · Last asked Aug 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
