Coding interview pattern
Explore level by level with a queue; the natural fit for shortest paths on unweighted graphs.
The idea
Breadth-first search explores a graph or tree outward in concentric layers: all nodes at distance one from the start, then all at distance two, and so on. It uses a queue (first in, first out) and a visited set, dequeuing a node, processing it, and enqueuing its unvisited neighbours. Because it reaches nodes in nondecreasing order of distance, BFS finds the shortest path in any unweighted graph, which is its signature use.
The level-by-level property is also what makes BFS the tool for tree level-order traversal and for any problem phrased as fewest steps, minimum moves, or nearest something. A common trick is to record the queue size at the start of each iteration so you can process exactly one full level at a time, which is how you tag each node with its depth or emit one list per tree level.
Multi-source BFS generalises the idea: seed the queue with several starting nodes at once and the search computes the distance from each node to its nearest source in a single pass. This solves grid problems like rotting oranges or walls and gates far more cleanly than running BFS from every cell separately.
Recognition
Cost
| Operation | Time | Space |
|---|---|---|
| Graph BFS | O(V + E) | O(V) |
| Grid BFS (r x c) | O(r*c) | O(r*c) |
| Tree level-order | O(n) | O(width) |
Worked example
Translatable skeleton
Language-agnostic on purpose. Translate it to your language of choice.
queue = [start]; visited = {start}; while queue: node = queue.popleft(); for nb in neighbours(node): if nb not in visited: visited.add(nb); queue.append(nb)Practice
Work these in order, easy to hard. Each is chosen to drill a different facet of the pattern.
| Problem | Level | Why it fits |
|---|---|---|
| Binary Tree Level Order Traversal | Medium | Process one level per queue snapshot. |
| Number of Islands | Medium | BFS flood fill on a grid. |
| Rotting Oranges | Medium | Multi-source BFS over time steps. |
| Word Ladder | Hard | BFS over a word-transformation graph. |
| Shortest Path in Binary Matrix | Medium | 8-directional grid BFS. |
| 01 Matrix | Medium | Multi-source BFS from all zeros. |
Avoid these
In the interview
Recognising the pattern is half the score; saying the right things out loud is the other half. These are the sentences that earn the signal.
Justify the choice of BFS over DFS by naming the keyword in the prompt, usually shortest, fewest, or minimum steps; BFS is correct for those on unweighted graphs precisely because it visits in order of distance.
Call out explicitly that you mark nodes visited at enqueue time, not dequeue time, and explain that this is what prevents the same node being queued twice and blowing up the runtime.
If the problem needs the depth or one list per level, mention the trick of snapshotting the queue size at the start of each loop so you process exactly one level at a time.
bfs traversal - level order traversal - shortest path unweighted graph
An external resource we recommend. AlgoExpert is not affiliated with us and we earn nothing from this link.
Keep going
Track a contiguous subarray or substring without re-scanning it from scratch.
Walk two indices toward or alongside each other to avoid a nested loop.
Halve the search space each step over any monotonic condition, not just sorted arrays.
Solve overlapping subproblems once, store the results, and build the answer up.
See all coding patterns, the coding questions for your role, and the behavioral round.