Reported November 2019
Bloombergtree

Rebuild Binary Tree from Level Order

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 edge case that kills a naive solution here is the trailing omission. Bloomberg reported this OA in November 2019, and it looks like a tree problem but it's really a queue and pointer problem. You get a level-order array with a null marker, rebuild the binary tree, then print preorder with the marker for every missing child. Trailing absent children can be left out, and a root that equals the marker is the whole input. If you blank on the reconstruction, StealthCoder runs invisibly during the live assessment and gives you a working solution as a safety net.

The problem

levelOrder is a compact breadth-first serialization of a binary tree. The first value is the root. For each non-null node removed from a queue, the next value (if present) is its left child and the following value (if present) is its right child. nullMarker means that child is absent and is never a real node value. Trailing absent children may be omitted.
Rebuild the tree and return its preorder serialization, writing nullMarker for every absent left or right child.

Function
levelOrderToPreorder(levelOrder: int[], nullMarker: int) → int[]

Examples
Example 1
levelOrder = [1,2,3,-1,4,-1,5]
nullMarker = -1
return = [1,2,-1,4,-1,-1,3,-1,5,-1,-1]
Node 1 has children 2 and 3; node 2 has only right child 4; node 3 has only right child 5. Preorder records each missing child as -1.

Constraints
1 <= levelOrder.length <= 100000.
The encoding is valid and completely consumed by the reconstruction rule.
Every real node value differs from nullMarker.
If the root equals nullMarker, it is the only input entry.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a queue plus a read index. Create the root, push it, then for each node you pop, consume the next value as its left child and the one after as its right. If the value equals nullMarker, leave that child empty. Otherwise create the node and enqueue it. Stop reading when the index passes the array end, because trailing absent children are omitted. That's the pitfall: don't assume every node gets two values. Check bounds before each read. Handle the root-is-marker case up front and return just [nullMarker]. With up to 100000 nodes, a skewed tree makes recursive preorder blow the stack in some languages, so use an explicit stack. Push right before left, and emit nullMarker whenever you hit an empty child. StealthCoder is your hedge if the live OA catches you mid-blank on the bounds logic.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Rebuild Binary Tree from Level Order 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 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. 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.

Rebuild Binary Tree from Level Order FAQ

What's the core trick in this Bloomberg tree rebuild problem?+

Use a queue and a running index into the array. Each popped node takes the next two values as left and right children. Marker means no child, anything else becomes a new node and gets enqueued. It's the standard BFS deserialization, just with omitted trailing values to guard against.

What edge case breaks most solutions?+

Reading past the end of the array. Trailing absent children may be omitted, so the last nodes in the queue might get zero or one value. Check the index before every child read. The other one is a root equal to nullMarker, which is the only entry.

Why might recursion fail on this problem?+

The input can have 100000 values, and a chain-shaped tree makes preorder recursion that deep. That can overflow the stack in many languages. Write preorder iteratively with an explicit stack, pushing right child first so left is processed first.

How should I write the preorder output?+

Visit a node, append its value, then handle left, then right. For any absent child, append nullMarker and don't descend. Example 1 shows this: node 2 has no left child, so a -1 follows it before 4. Leaf nodes emit two markers.

How do I prepare for this in 48 hours?+

Practice BFS tree deserialization once, then iterative preorder once. Write both from scratch and test on a skewed tree, a single-node tree, and a marker-only root. That covers the cases this problem hits. Don't memorize, just get the queue and index loop automatic.

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