Tools
EXAMS TODAY!
Get Notes Now
Juspay InterviewHigh FrequencyMedium-Hard

Locking the Tree of Space: Complete Guide for Juspay Interview

Master Juspay's signature coding challenge with this comprehensive breakdown of the "Locking and Unlocking N-Ary Tree" problem. Updated for 2026 hiring season.

Why This Problem Matters:

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.

Registration Status: Closed (HackerEarth Round)

The public HackerEarth application window closed on September 20, 2026. If you received your assessment link or are invited to the 24-Hour Learn-A-Thon / Round 2, use the prep drill below.

Problem Statement

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.

Implement three operations:

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)

Solution Approaches: Brute Force vs Optimal

Understanding the time complexity trade-offs

Optimal Approach - O(H) per operation

Use parent pointers, locking states, and subtree counters to eliminate full tree traversals.

Key Data Structures:

  • • parent[node] - Direct parent pointer
  • • lockedBy[node] - User ID or -1 if unlocked
  • • lockedDescendants[node] - Count of locked nodes in subtree
✓Lock: Check ancestors only using parent pointers - O(H)
✓Unlock: Update counters along ancestor path - O(H)
✓Upgrade: Check count, collect locked nodes, unlock them - O(H + M) where M is locked descendants

Why 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.

Battle-Tested Solutions

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;
    }
};
C++ Tips: Use static arrays or unordered_map for O(1) node lookups. The parent pointer approach eliminates the need for DFS in most operations.

Common Mistakes to Avoid

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.

Can You Pass Part-A First?

70% fail the MCQ screening before seeing this problem.

Test Day Prep Checklist

Learn about all 3 rounds + the confusing Learn-A-Thon.

Ready to Ace Your Juspay Interview?

Practice this problem multiple times until you can code it from memory.

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