Reported December 2025
Scale AItree

Lowest Common Ancestor in a General Tree

Reported by candidates from Scale AI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Scale AI OA. Under 2s to a working solution.
Founder's read

The data structure here is a children adjacency list, and that's the whole game. Scale AI reported this OA in December 2025: find the lowest common ancestor of two nodes in a rooted general tree, not a binary one. Node 0 is the root, n goes up to 100000, and a node counts as its own ancestor. It's a tree problem that looks easy until recursion depth or a missed edge case bites you. If you blank during the live assessment, StealthCoder runs invisibly on your desktop and hands you a working solution. Know the trick first, though, and you won't need it.

The problem

You are given a rooted general tree whose nodes are labeled from 0 through n - 1.
The array children represents the tree: children[i] contains every direct child of node i. Given two node labels p and q, return their lowest common ancestor.
The lowest common ancestor is the deepest node that is an ancestor of both targets. A node is an ancestor of itself.

Function
lowestCommonAncestor(children: int[][], p: int, q: int) → int

Examples
Example 1
children = [[1,2,3],[4,5],[],[6],[],[],[]]
p = 4
q = 5
return = 1
Nodes 4 and 5 are both direct children of node 1, so their lowest common ancestor is 1.
Example 2
children = [[1,2,3],[4,5],[],[6],[],[],[]]
p = 4
q = 6
return = 0
Node 4 lies below child 1, while node 6 lies below child 3. Their paths first meet at the root, node 0.
Example 3
children = [[1,2,3],[4,5],[],[6],[],[],[]]
p = 3
q = 6
return = 3
Node 3 is an ancestor of node 6 and of itself, so it is the lowest common ancestor.

Constraints
1 <= n = children.length <= 100000.
The node labels are exactly 0, 1,..., n - 1, and node 0 is the root.
Every node other than 0 appears exactly once across all child lists, every listed label is valid, and all nodes are reachable from the root.
0 <= p, q < n. The targets may be equal.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The input gives you children, not parents. So build a parent array in one pass: for each node i, for each child c, set parent[c] = i. Then compute depth with a BFS from node 0. Lift the deeper of p and q up until depths match, then move both up together until they meet. That's O(n) time and O(n) space. The alternative is a post-order DFS that returns a node when it finds p or q, but with n at 100000 a skewed tree can blow the recursion stack in many languages, so go iterative. Pitfalls: p equals q (answer is p), and p being an ancestor of q (answer is p). The parent-pointer approach handles both without special cases. If the live OA freezes your head, StealthCoder is the hedge that reads the problem and gives you the iterative version.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Lowest Common Ancestor in a General 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Scale AI's OA.

Scale AI reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Lowest Common Ancestor in a General Tree FAQ

How hard is the Scale AI lowest common ancestor question really?+

Easy to medium. The idea is standard, but the input is a children list for a general tree, and n reaches 100000. The difficulty is picking an iterative approach and handling the edge cases, not inventing a new algorithm.

What's the trick to solving it fast?+

Invert the children array into a parent array, compute depths with BFS from node 0, then lift the deeper node until depths match. Walk both up together until they're the same node. No special cases needed.

Should I use recursion or iteration?+

Iteration. A chain-shaped tree with 100000 nodes can overflow the call stack in Python or Java. BFS for depth and a parent-pointer walk avoid recursion entirely and stay linear.

What edge cases does this problem test?+

p equals q, where the answer is that node. One target being an ancestor of the other, like Example 3 where the answer is 3. Also a single-node tree, and a deep skewed tree that stresses stack depth.

How do I prepare for this in 48 hours?+

Write the parent-array plus depth-lift solution from scratch twice. Then test it on the three given examples and a long chain. Also know the binary tree LCA recursion, since interviewers sometimes follow up by asking how it generalizes.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Scale AI.

OA at Scale AI?
Invisible during screen share
Get it