Master Juspay's signature coding challenge with this comprehensive breakdown of the "Locking and Unlocking N-Ary Tree" problem. Updated for 2026 hiring season.
This question appears in 90%+ of Juspay coding challenges. Students who receive the Juspay assessment link search for this problem within hours. Understanding this problem is critical for your Juspay interview success.
Understanding the Locking and Unlocking Mechanism
You are given an N-ary tree where each node can be in one of two states: locked or unlocked. Initially, all nodes are unlocked.
1. lock(node, userId)
Lock the given node for the user. Returns true if successful, false otherwise.
Constraint: Cannot lock if any ancestor or descendant is already locked.
2. unlock(node, userId)
Unlock the given node. Returns true if successful, false otherwise.
Constraint: Only the user who locked the node can unlock it.
3. upgrade(node, userId)
Lock the node and unlock all locked descendants. Returns true if successful.
Constraints: Node must be unlocked, at least one descendant must be locked, no ancestor can be locked, all locked descendants must be locked by the same userId.
📝 Example Input Trace:
Tree Structure:
1
/|\
2 3 4
/| |\
5 6 7 8
Operations:
lock(3, user1) → true (3 is now locked)
lock(1, user2) → false (descendant 3 is locked)
unlock(3, user2) → false (user2 didn't lock it)
unlock(3, user1) → true (3 is now unlocked)
lock(5, user1) → true
lock(6, user1) → true
upgrade(2, user1)→ true (2 is locked, 5 and 6 unlocked)Understanding the time complexity trade-offs
Use parent pointers, locking states, and subtree counters to eliminate full tree traversals.
Key Data Structures:
parent[node] - Direct parent pointerlockedBy[node] - User ID or -1 if unlockedlockedDescendants[node] - Count of locked nodes in subtreeWhy it works: Height H is typically log(N) for balanced trees, giving O(Q × log N) total complexity - fast enough for all Juspay test cases.
Production-ready code in C++, Java, and Python
class TreeNode {
public:
int id;
TreeNode* parent;
vector<TreeNode*> children;
int lockedBy; // -1 if unlocked, else userId
int lockedDescCount; // count of locked descendants
TreeNode(int nodeId) : id(nodeId), parent(nullptr),
lockedBy(-1), lockedDescCount(0) {}
};
class LockingTree {
private:
unordered_map<int, TreeNode*> nodes;
bool hasLockedAncestor(TreeNode* node) {
TreeNode* curr = node->parent;
while (curr != nullptr) {
if (curr->lockedBy != -1) return true;
curr = curr->parent;
}
return false;
}
void updateAncestorCounts(TreeNode* node, int delta) {
TreeNode* curr = node->parent;
while (curr != nullptr) {
curr->lockedDescCount += delta;
curr = curr->parent;
}
}
void collectLockedDescendants(TreeNode* node, int userId,
vector<TreeNode*>& locked) {
if (node->lockedBy == userId) {
locked.push_back(node);
}
for (TreeNode* child : node->children) {
collectLockedDescendants(child, userId, locked);
}
}
public:
LockingTree(vector<int>& parent) {
int n = parent.size();
for (int i = 0; i < n; i++) {
nodes[i] = new TreeNode(i);
}
for (int i = 0; i < n; i++) {
if (parent[i] != -1) {
nodes[i]->parent = nodes[parent[i]];
nodes[parent[i]]->children.push_back(nodes[i]);
}
}
}
bool lock(int nodeId, int userId) {
TreeNode* node = nodes[nodeId];
if (node->lockedBy != -1 ||
node->lockedDescCount > 0 ||
hasLockedAncestor(node)) {
return false;
}
node->lockedBy = userId;
updateAncestorCounts(node, 1);
return true;
}
bool unlock(int nodeId, int userId) {
TreeNode* node = nodes[nodeId];
if (node->lockedBy != userId) {
return false;
}
node->lockedBy = -1;
updateAncestorCounts(node, -1);
return true;
}
bool upgrade(int nodeId, int userId) {
TreeNode* node = nodes[nodeId];
if (node->lockedBy != -1 ||
node->lockedDescCount == 0 ||
hasLockedAncestor(node)) {
return false;
}
vector<TreeNode*> lockedDesc;
collectLockedDescendants(node, userId, lockedDesc);
if (lockedDesc.size() != node->lockedDescCount) {
return false;
}
for (TreeNode* desc : lockedDesc) {
desc->lockedBy = -1;
}
node->lockedBy = userId;
node->lockedDescCount = 0;
updateAncestorCounts(node, 1);
return true;
}
};unordered_map for O(1) node lookups. The parent pointer approach eliminates the need for DFS in most operations.Learn from others' errors in the Juspay interview
Forgetting to update ancestor counts
After locking/unlocking, you MUST update the lockedDescCount for all ancestors.
Not checking userId in upgrade()
All locked descendants must be locked by the SAME userId.
Using full DFS for lock/unlock
Don't traverse the entire subtree — use the lockedDescCount counter for O(H) verification.
Practice this problem multiple times until you can code it from memory.
Master 101 curated LeetCode problems with our structured learning path. Track progress, identify weak areas, and build interview confidence.
Start Practicing →Practice 200+ aptitude questions across TCS, Infosys, Wipro, and more. Timed tests with instant feedback and detailed analytics.
Take Practice Test →Keywords: Juspay interview questions 2026, Locking the Tree of Space solution, Juspay coding challenge, N-ary tree locking, tree locking algorithm, Juspay hiring challenge, lock unlock upgrade tree, Juspay placement questions