Lowest Common Ancestor in a Parent Tree
Reported by candidates from BlackRock's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole BlackRock question lives in one array: parent[]. No node objects, no children lists, just each node pointing up at its parent. BlackRock candidates reported this one in September 2026, and it's a lowest common ancestor problem on a rooted tree. The trick is that you only ever walk upward, which makes it simpler than the binary tree version you've memorized. If you've seen LCA before, this is a quick one. If you haven't, the logic is short and you can own it tonight. And if you freeze during the live OA, StealthCoder is the safety net that runs invisibly on your screen.
The problem
A rooted tree with nodes 0 through n - 1 is represented by parent. The root is node 0, parent[0] = -1, and for every other node i, parent[i] is its direct parent. Given nodes p and q, return their lowest common ancestor: the common ancestor farthest from the root. A node is an ancestor of itself. Function lowestCommonAncestor(parent: int[], p: int, q: int) → int Examples Example 1 parent = [-1,0,0,1,1,2,2] p = 3 q = 4 return = 1 Nodes 3 and 4 are both direct children of node 1. Example 2 parent = [-1,0,0,1,1,2,2] p = 3 q = 6 return = 0 The two nodes lie in different root subtrees. Constraints 1 <= parent.length <= 100000. parent[0] = -1. For i > 0, 0 <= parent[i] < i. 0 <= p, q < parent.length.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The data structure is the parent array itself. Two clean approaches. First, build a set of every ancestor of p by walking up until you hit -1, then walk up from q and return the first node already in the set. That's O(n) time and O(n) space. Second, compute depths, lift the deeper node until both depths match, then move both up together until they meet. Since parent[i] < i, you can fill depth in a single forward pass with depth[i] = depth[parent[i]] + 1, no recursion needed. The common pitfall is recursing on a 100000 node chain and blowing the stack. Another is forgetting a node is its own ancestor, so p == q or p being an ancestor of q must return p. Keep it iterative. If your mind goes blank mid-assessment, StealthCoder is the hedge that gives you the solution in real time.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Lowest Common Ancestor in a Parent Tree cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass BlackRock's OA.
BlackRock reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Lowest Common Ancestor in a Parent Tree FAQ
How hard is the BlackRock LCA parent tree question really?+
Easy to medium. There's no tree construction and no tricky data structure. You walk up a parent array. If you know the ancestor-set method, you can code it in under ten lines. The edge cases are what trip people up, not the idea.
What's the simplest approach that passes?+
Walk from p to the root, storing every node in a set, including p. Then walk from q upward and return the first node found in the set. It's O(n) time and space, and it handles the case where one node is an ancestor of the other.
Is there a way to use O(1) extra space?+
Yes. Compute the depth of p and q by walking up, lift the deeper one until depths are equal, then step both up together until they match. Depth walks cost O(n) per query, which is fine for a single query, and you use no extra memory.
What edge cases should I test?+
Test p equal to q, p equal to 0, and q being a direct ancestor of p. Also test a single node tree where parent has length 1, and a long chain of 100000 nodes to confirm you aren't using recursion that overflows the stack.
How do I prepare for this in 48 hours?+
Write the ancestor-set solution from scratch twice, then the depth-lifting version once. Run the two examples by hand. Then do the classic binary tree LCA once so you can recognize the same idea in a different input form.