Reported September 2026
Bloombergtree

Lowest Common Ancestor in an N-ary Tree

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

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

The mistake that sinks a first attempt on this Bloomberg OA, reported in September 2026, is treating the parent array like a normal binary tree and hunting for children. You don't need children at all. You get a parent array, two labels, and up to 200000 nodes. It's a tree problem that's really a pointer-walking problem. If you've seen LCA before, this is the easy version. If you blank under the clock, StealthCoder runs invisibly on your desktop as a safety net for the live assessment.

The problem

A rooted N-ary tree has nodes labeled from 0 through n - 1. It is encoded by parent, where parent[root] = -1 and every other value gives the node's direct parent.
Given two valid node labels p and q, return their lowest common ancestor: the deepest node that is an ancestor of both. 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,3]
p = 7
q = 4
return = 1
Node 1 is the deepest shared ancestor of nodes 7 and 4.
Example 2
parent = [-1,0,0,1,1,2,2]
p = 5
q = 6
return = 2
Both queried nodes are direct children of node 2.

Constraints
1 <= parent.length <= 200000.
Exactly one entry is -1; every other entry is a valid node label.
The parent relationships form one connected acyclic rooted tree.
0 <= p, q < parent.length.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that parent pointers let you climb. Walk up from p and store every ancestor in a hash set, including p itself. Then walk up from q and return the first node already in the set. That's O(h) time and space. The pitfall is skipping the self-ancestor rule. If p is an ancestor of q, the answer is p, and you only get that if you add p to the set before climbing. The second pitfall is recursion. With 200000 nodes, a skewed tree is a 200000-deep chain, so any recursive approach risks a stack overflow. Stay iterative. The no-extra-space option is to compute both depths, lift the deeper node until depths match, then step both up together until they meet. If you freeze on the OA, StealthCoder is the hedge that hands you this loop while you keep typing.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Lowest Common Ancestor in an N-ary 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Lowest Common Ancestor in an N-ary Tree FAQ

What's the trick to the Bloomberg N-ary LCA question?+

Use the parent array directly. Climb from p to the root and record every ancestor, including p. Then climb from q and return the first node you've already seen. No child lists and no tree building are needed, and it runs in O(h).

How hard is this really?+

Easy to medium. The idea is short once you notice the parent pointers do the work. The risk is edge cases: p equals q, one node being the ancestor of the other, or a skewed tree at the 200000 limit.

Will recursion pass the constraints?+

Probably not safely. A chain of 200000 nodes means a recursion depth of 200000, which can overflow the stack in many languages. Use a loop to climb parent pointers. Both the set approach and the depth-matching approach are naturally iterative.

Is there a way to do it without a hash set?+

Yes. Compute the depth of p and q by climbing to the root. Lift the deeper node up until both depths match. Then move both up one step at a time until they're the same node. That uses O(1) extra space.

How do I prepare in 48 hours?+

Write the set-based climb from memory, then the depth-matching version. Test p equals q, p as ancestor of q, and a long chain. Check that example 1 returns 1 and example 2 returns 2. That covers nearly every variant of this question.

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

OA at Bloomberg?
Invisible during screen share
Get it