Reported October 2022
Figmabreadth first search

Breadth-First Values of a Binary Tree

Reported by candidates from Figma's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Figma OA. Under 2s to a working solution.
Founder's read

Figma reported this one in October 2022, and the empty-tree line in the statement is the only thing that bites: return an empty array when the root is null. The rest is a plain breadth-first traversal. Visit level by level, left to right, and flatten the values into one list. It looks too easy for an OA, which is exactly why people rush it and fumble the edge case. If you blank on the queue mechanics when the timer is running, StealthCoder is the safety net that sits invisibly on your screen and hands you the working solution.

The problem

Given the root of a binary tree, return its node values in breadth-first order.
Visit nodes level by level from top to bottom, and from left to right within each level. Return an empty array for an empty tree.

Function
breadthFirstValues(root: TreeNode) → int[]

Examples
Example 1
root = [3,9,20,null,null,15,7]
return = [3,9,20,15,7]
The traversal visits the root, then 9 and 20, then 15 and 7.
Example 2
root = [1,2,3,4,5]
return = [1,2,3,4,5]
Values on the same level are emitted from left to right.
Example 3
root = []
return = []
An empty tree has no values to visit.

Constraints
The tree contains at most 100000 nodes.
Each node value fits in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is BFS with a queue. Push the root, then loop: pop a node, append its value, push its left child, then its right child, skipping nulls. Left-before-right gives you the left-to-right order within each level, and the queue gives you top-to-bottom. You don't need level boundaries because the output is flat. The pitfalls are small but real. Check for a null root first and return []. Don't use an array with shift() in a language where that's O(n), because with up to 100000 nodes you'll turn linear into quadratic. Use a deque or a head index instead. Also don't go recursive depth-first, since a skewed tree of 100000 nodes can blow the stack. Time is O(n), space is O(w) for the widest level. If you freeze during the live OA, StealthCoder is the hedge that gives you the clean queue loop.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Breadth-First Values of a Binary Tree 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as binary tree 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 Figma's OA.

Figma reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Breadth-First Values of a Binary Tree FAQ

How hard is Figma's breadth-first values question really?+

Easy. It's the textbook level-order traversal minus the grouping by level. If you've written a BFS with a queue once, you can write this in a few minutes. The only real risk is skipping the empty-tree case or using a slow queue.

What's the trick to this problem?+

Use a queue. Start with the root, pop from the front, record the value, then enqueue left and then right child if they exist. Enqueue order is what produces left-to-right output. No recursion and no level tracking needed.

Why not just use DFS?+

Plain DFS visits depth first, so the order comes out wrong unless you track depth and bucket values by level. That's extra work. Recursion also risks stack overflow on a skewed tree with 100000 nodes. BFS is simpler and safer here.

What edge cases should I test?+

Test the empty tree, which must return an empty array. Test a single node, a fully skewed tree, and a tree with negative or large 32-bit values. Example 1 with null gaps is a good check that you skip missing children correctly.

How do I prepare for this in 48 hours?+

Write level-order traversal from memory three times in your main language, using a proper deque or index pointer. Then do the variant that returns values grouped by level. Once the queue loop is automatic, this problem is done.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Figma.

OA at Figma?
Invisible during screen share
Get it