Reported November 2019
Bloombergbreadth first search

Binary Tree Spiral 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 first sentence of this Bloomberg OA from November 2019 hinges on one thing: a queue. Binary Tree Spiral Level Order asks you to return node values level by level, flipping direction each row. It's a tree problem, but the real work is breadth-first traversal with a direction toggle. If you've seen zigzag level order before, this is a ten-minute job. If you haven't, it's easy to overthink. The OA is theater, and this one has a short script. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but the pattern below should get you most of the way there.

The problem

Return the values of a binary tree one level at a time in alternating directions.
For this exercise, assume the root level is traversed left to right, the next level right to left, and directions alternate thereafter. Group values by their distance from the root and omit missing children. Preserve node positions even when values repeat. An empty tree returns an empty list.

Function
zigzagLevels(root: TreeNode) → int[][]

Examples
Example 1
root = [3,9,20,null,null,15,7]
return = [[3],[20,9],[15,7]]
The root is left-to-right, the second level is right-to-left, and the third returns to left-to-right.
Example 2
root = [1,2,3,4,null,null,5]
return = [[1],[3,2],[4,5]]
Null links are skipped; the second level is [3,2] and the third is [4,5].
Example 3
root = []
return = []
An empty tree has no levels.

Constraints
For this exercise, assume a finite acyclic binary tree with 0 through 500 nodes.
-10^6 <= node.val <= 10^6.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Run a standard BFS with a queue. At each level, record the queue size, pop exactly that many nodes, and collect their values into a list. Push left child then right child every time, no matter the direction. Keep a boolean flag that flips after each level. When the flag says right-to-left, reverse the level list before appending it, or write into a preallocated array from the back. The common pitfall is changing the order you enqueue children based on direction. That breaks the next level's grouping. Another trap is forgetting the empty tree, which must return an empty list, not [[]]. Check Example 2: level two is [3,2] and level three is [4,5], with null links skipped. Complexity is O(n) time and O(n) space. With 500 nodes max, recursion would also work, but BFS is cleaner. If you freeze live, StealthCoder can supply the loop as a hedge, but you only need about fifteen lines.

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 Binary Tree Spiral 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as binary tree zigzag level order traversal. 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. 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.

Binary Tree Spiral Level Order FAQ

What's the trick in Binary Tree Spiral Level Order?+

Do a normal level-order BFS and only change how you store each level. Keep a flag that flips every level. On right-to-left levels, reverse the collected values. Never change the order you enqueue children, or the next level's grouping breaks.

How hard is this Bloomberg OA question really?+

It's a medium at worst. The traversal is standard BFS and the only twist is the direction toggle. If you know level order, you add one flag and a reverse. Most candidates who stumble do so on edge cases like the empty tree.

Should I use BFS or DFS here?+

BFS is the natural fit because the problem groups values by distance from the root. DFS works if you pass the depth and insert into the right level, reversing on odd depths. With at most 500 nodes, both are fine, but BFS is easier to get right under pressure.

What edge cases should I test?+

Test the empty tree, which returns an empty list. Test a single node, which returns [[val]]. Test a skewed tree where each level has one node. Also check Example 2, where null links get skipped and levels have uneven sizes.

How do I prepare for this in 48 hours?+

Write level-order traversal from memory three times until it's automatic. Then add the direction flag and the reverse step. Run Examples 1 and 2 by hand. Spend the rest of your time on related tree problems like right side view and level averages.

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