What they test: graph modeling, shortest path in unweighted graphs, queue discipline, and visited invariants.
What you should say out loud:
- "BFS explores level-by-level using a queue."
- "Invariant: when a node is dequeued, we have the shortest distance to it (unweighted graph)."
- "We mark visited when enqueueing to avoid duplicates and cycles."
Breadth-First Search traverses a graph in increasing distance from a start node. It’s the default tool for shortest paths in unweighted graphs and "k hops away" problems.
- Traverse all nodes reachable from a source
- Compute shortest path (fewest edges) in unweighted graphs
- Build level/layer partitions (distance buckets)
- Visited invariant: each node enters the queue at most once.
- Distance invariant: first time we reach a node is via the shortest number of edges.
- Level invariant (if using levels): nodes in the same level share equal distance from start.
Queue order ensures we process all nodes at distance d before any node at distance d+1.
- "Return BFS traversal order starting from node s."
- "Return nodes grouped by distance (levels)."
- "Return shortest path between s and t in an unweighted graph."
- Time: O(V + E)
- Space: O(V) (queue + visited + parent/dist maps)
- Use adjacency list (map or slice-of-slices) and a slice-based queue (
[]intwith head index) to avoidcontainer/listoverhead. - Mark visited on enqueue, not dequeue.
- For shortest path reconstruction, maintain
parentmap/slice and rebuild by walking parents backward.
- Marking visited too late → duplicates and possible blow-ups on dense graphs.
- Assuming graph is connected.
- Not defining behavior for start node not present / empty graph.
- Binary Tree Level Order Traversal (LC #102)
- Shortest Path in Binary Matrix (LC #1091)
- Rotting Oranges (LC #994)
- Word Ladder (LC #127)
- Multi-source BFS
- Bidirectional BFS
- BFS on grid / implicit graph
- BFS that stops early when target found
Implement these functions/types in this package (exact names used by tests):
package bfs
type Graph struct {
// your representation
}
func NewGraph() *Graph
func (g *Graph) AddEdge(u, v int)
func (g *Graph) AddUndirectedEdge(u, v int)
func BFS(g *Graph, start int) []int
func BFSWithLevels(g *Graph, start int) [][]int
// ShortestPath returns (path, distance). If no path: (nil, -1).
func ShortestPath(g *Graph, start, target int) ([]int, int)