Linked List of Binary Tree Nodes at a Given Level
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A tree, a level number, and a left-to-right list. That's the whole Bloomberg question reported in October 2019, and it's a BFS or DFS depth check dressed up with a linked-list story. You return the values at one zero-based depth, or an empty result if the tree isn't that deep. If your OA invite lands in the next day or two, this is a warm-up-sized problem that still trips people on edge cases. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the logic here is short enough to hold in your head.
The problem
Collect the binary-tree nodes at zero-based depth level from left to right, as if appending them to a new singly linked list. Return that list's values. Return empty when the level is absent. Function nodesAtLevel(root: TreeNode, level: int) → int[] Examples Example 1 root = [1,2,3,4,5,null,6] level = 2 return = [4,5,6] The depth-two nodes appear left-to-right as 4,5,6. Constraints The tree contains at most 10^5 nodes. level >= 0.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you only care about one depth. Do a level-order traversal with a queue, process the tree one layer at a time, and when the layer index equals level, return its values. If the queue empties first, return empty. The alternative is DFS carrying a depth counter, visiting left before right, and appending a value whenever depth equals level. Both give left-to-right order and run in O(n). Pitfalls: the null entries in the sample array aren't real nodes, so don't enqueue them. With up to 10^5 nodes, a skewed tree can make recursive DFS blow the stack, so prefer BFS or an iterative approach. Also handle a null root and a level beyond the height. If you freeze during the live OA, StealthCoder can hand you the BFS skeleton, but you should be able to write it from this paragraph alone.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Linked List of Binary Tree Nodes at a Given Level 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 StealthCoderRelated leaked OAs
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.
Linked List of Binary Tree Nodes at a Given Level FAQ
How hard is the Bloomberg nodes-at-level problem really?+
Easy. It's a standard tree traversal with one extra condition. The difficulty is in the details: empty trees, a level deeper than the tree, and preserving left-to-right order. If you've written a level-order traversal before, you can finish this in a few minutes.
What's the trick to solving it?+
Track depth. In BFS, process the queue one layer at a time and stop when the layer index equals level. In DFS, pass depth as a parameter and append a value when depth matches. Visit left child before right child so the order comes out correct.
Should I use BFS or DFS?+
BFS is safer. With up to 10^5 nodes, a skewed tree can overflow the recursion stack in DFS. BFS also lets you stop early once you've collected the target layer. DFS works fine if you do it iteratively or know the depth is bounded.
What should I return if the level doesn't exist?+
An empty array. The problem says to return empty when the level is absent. With BFS, that happens naturally when the queue runs out before you reach the target depth. Make sure a null root also returns empty instead of crashing.
How do I prepare for this in 48 hours?+
Write level-order traversal from memory twice, then write the depth-parameter DFS version. Test on a one-node tree, a skewed tree, and a level past the height. That covers every trap in this problem. Then spend the remaining time on other tree traversal variants.