Reported November 2019
Bloombergbreadth first search

N-ary Tree Right Side View

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 November 2019, is walking only the last child down every branch. That works on a clean tree and fails the moment a deep left subtree is the only thing on a level. The task: given values and child index lists with root 0, return the rightmost node's value on each level. It's a tree problem with a level-order backbone. If you blank during the live assessment, StealthCoder can sit invisibly on your screen as a safety net and hand you the traversal.

The problem

Node i has value values[i] and ordered child indices children[i]; root is 0. Return the value of the rightmost node on every level from top to bottom.

Function
naryRightSideView(values: int[], children: int[][]) → int[]

Examples
Example 1
values = [1,2,3,4,5]
children = [[1,2,3],[4],[],[],[]]
return = [1,4,5]
Indices 0, 3, and 4 are rightmost on their levels.

Constraints
Children form one rooted tree when values is nonempty.
At most 10^5 nodes.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is level-order traversal (BFS). Push the root, and on each level record the last node you process. With the children arrays, iterate each node's children in order and enqueue them, so the final node dequeued per level is the rightmost. The common pitfall is the one above: following children[i][last] repeatedly. That misses levels where the right branch ends early but a left branch keeps going. A DFS also works if you visit the rightmost child first and record the first node seen at each new depth. With up to 10^5 nodes, avoid recursion if your language has a shallow stack, since a chain tree gets deep. Handle empty values by returning an empty list. Time is O(n), space is O(n). If your mind goes blank on the live OA, StealthCoder is the hedge: it reads the problem and gives you the queue loop.

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 N-ary Tree Right Side View 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 binary tree right side view. 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.

N-ary Tree Right Side View FAQ

What's the trick for N-ary Tree Right Side View?+

Do a level-order BFS and keep the last node of each level. Don't just follow the last child down, because a deeper left branch can supply the rightmost node on lower levels. Process each level by its queue size, then append the final value.

Can I use DFS instead of BFS?+

Yes. Visit children from right to left, and when you reach a depth you haven't recorded yet, save that node's value. The first node seen at each depth is the rightmost. With 10^5 nodes, a chain-shaped tree can overflow recursion in some languages, so consider an explicit stack.

How hard is this really?+

Easy to medium. It's a standard BFS variant with an index-based tree instead of node objects. Most of the difficulty is the right-side-only mistake and reading the children array correctly. If you know level-order traversal, you can finish it quickly.

What edge cases should I test?+

Test an empty values array, a single node, a chain going left only, and a tree where the rightmost node of a level is not a child of the previous level's rightmost node. Example 1 gives [1,4,5], which shows that index 4 sits under node 1, not node 3.

How do I prepare for this in 48 hours?+

Write level-order BFS from memory twice, once with a queue size loop and once with DFS tracking depth. Then run Example 1 by hand. Practice reading adjacency-style input like children[i], since the format is what trips people up more than the algorithm.

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