Reported September 2022
Bloombergbreadth first search

Populate Next Right Pointers

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

Strip the tree vocabulary off this Bloomberg OA, reported September 2022, and it's a level-order traversal that remembers who came right before whom. Given left and right child index arrays, you return each node's right neighbor on the same level, or -1 at the end of a level. It's the classic next-right-pointer problem, just encoded as arrays instead of node objects. With up to 10^5 nodes, you need a clean linear pass. If your head goes blank mid-assessment, StealthCoder runs invisibly as a safety net while you work. Know the BFS shape before you start and this one is quick.

The problem

Node indices are 0 through n-1; root is 0. left[i] and right[i] are child indices or -1.
Return an array where result[i] is the next node to the right of i on the same level, or -1 for the level's final node.

Function
populateNextRight(left: int[], right: int[]) → int[]

Examples
Example 1
left = [1,3,-1,-1,-1,-1,-1]
right = [2,4,5,-1,-1,6,-1]
return = [-1,2,-1,4,5,-1,-1]
Each level is linked left-to-right by node index.

Constraints
Arrays have equal length and encode one binary tree.
At most 10^5 nodes.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: run BFS from node 0 and process the queue one level at a time. Record the level size first, then walk that many nodes. For each node popped, if it isn't the last one in the level, its answer is the next node in the queue. Otherwise it stays -1. Push left[i] and right[i] when they aren't -1. Initialize the result array to -1 so you only write the non-final nodes. The common pitfall is mixing up indices and values. The output is indexed by node id, not by position in the level. Another trap is using recursion on a skewed tree of 10^5 nodes, which can blow the stack. Stay iterative. Time is O(n), space is O(n). If you freeze on the level-boundary logic during the live OA, StealthCoder can surface the working version so you can check yours against it.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Populate Next Right Pointers 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as populating next right pointers in each node ii. If you have time before the OA, drill that.

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Populate Next Right Pointers FAQ

What's the trick to Populate Next Right Pointers here?+

Level-order BFS. Snapshot the queue size at the start of each level, then for each node in that level, set result[node] to the next queued node unless it's the last one. The last one stays -1. That's the whole solution, linear time.

How hard is this one really?+

Easy to medium. The only real work is translating the array encoding into a BFS. If you've seen level-order traversal before, you can finish it in a few minutes. The edge cases are a single-node tree and unbalanced shapes.

Can I use DFS instead of BFS?+

You can, by tracking depth and the last-seen node per depth, but it's messier. Recursion also risks stack overflow at 10^5 nodes on a skewed tree. BFS with a queue is safer and simpler to reason about.

Is the result indexed by position or node id?+

Node id. result[i] is the right neighbor of node i. In the example, node 1 maps to 2 and node 3 maps to 4. Writing results by level position instead of node index is the most common wrong answer.

How do I prepare for this in 48 hours?+

Write BFS level-order traversal from memory twice, once with a queue and level-size loop. Then adapt it to index arrays with -1 sentinels. Test on a one-node tree and a left-skewed chain. That covers nearly everything this problem can throw at you.

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