Juspay - Tree of Space - Locking and Unlocking N-Ary Tree

You are given a complete, balanced N-Ary Tree and must support Q queries. There are 3 kinds of queries. Return true or false depending on whether the query was successful.

  1. Lock(v, id) - Lock vertex v for user - id
  2. Unlock(v, id) - If vertex v is locked by the same id, unlock it.
  3. Upgrade(v, id) - If v is unlocked and has at least one locked vertex in it's subtree and every locked vertex in the subtree of v is locked by id, unlock them and lock v instead.

Further, here are some additional constraints

  1. A vertex cannot be locked if it has any locked ancestors or descendants, by any ID.
  2. When a vertex is upgraded, it's locked descendants are automatically unlocked.
  3. An upgrade operation is not possible if the vertex is already locked or has any locked ancestors
  4. An unlock operation is only possible if the vertex is already locked and locked by the same id

P.S. - The question was very badly stated and not a single constraint was mentioned openly on the problem statement. A lot of these constraints had to be deduced by supplying custom input and observing the expected output. There were no constraints on the size of the variables.
The problem without the constraint of the ID and Upgrade function is well known and explained here.

Comments (21)