BFS/DFS Graph and Tree Traversal and Shortest Paths
Asked of: Software Engineer
Last updated

What's being tested
Candidates must demonstrate correct use of BFS and DFS for traversal, reachability, and component counting, plus shortest-path techniques (unweighted BFS, Dijkstra) under constraints. Interviewers probe algorithmic tradeoffs (time/space), correctness with blocked/forbidden nodes, and iterative vs recursive implementations to avoid stack overflow.
Patterns & templates
-
BFS for shortest paths in unweighted graphs — use
dequequeue, mark visited on enqueue, time O(V+E), space O(V). -
DFS (recursive or explicit stack) for connectivity and nested structures; prefer iterative stack to avoid recursion depth issues.
-
Dijkstra with
heapqfor weighted shortest paths; complexity O((V+E) log V); store distances and parents for path reconstruction. -
Multi-criteria shortest path: encode tuple cost (danger_count, steps) and use lexicographic comparison in priority queue or use 0-1 BFS for binary costs.
-
Remove/ignore blocked nodes by pre-marking in
setor deleting adjacency entries before traversal. -
Connected clusters (geometric): build adjacency by threshold distance squared to avoid
sqrt, deduplicate coordinates with aset, then BFS/DFS for components. -
Deleting in a binary search tree: handle leaf, single-child, two-children cases — replace with inorder successor (min in right subtree) and adjust pointers.
Common pitfalls
Pitfall: Marking visited only on pop instead of on enqueue causes duplicate enqueues and exponential blowup on dense graphs.
Pitfall: Using
sqrtfor many distance checks costs CPU and risks floating error — compare squared distances instead.
Pitfall: Recursing on deeply nested lists/trees without converting to an iterative stack risks stack overflow on large inputs.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Compute a Depth-Weighted Sum of a Nested ListGoogle · Software Engineer · Onsite · medium
- Determine All Players with Fixed RankingsGoogle · Software Engineer · Onsite · medium
- Count Connected Clusters of Two-Dimensional PointsGoogle · Software Engineer · Technical Screen · medium
- Minimize Direction Violations in a Directed Road NetworkGoogle · Software Engineer · Onsite · medium
- Find Players with Uniquely Determined RankingsGoogle · Software Engineer · Onsite · medium
- Determine Reporting RelationshipsGoogle · Software Engineer · Technical Screen · medium
- Validate parent array forms a treeGoogle · Software Engineer · Technical Screen · medium
- Compute shortest delivery route with dangerous stopsGoogle · Software Engineer · Onsite · medium
- Track Island Counts as Land Is AddedGoogle · Software Engineer · Onsite · medium
- Find right-side view of binary treeGoogle · Software Engineer · Technical Screen · easy
- Group movies via graph traversalGoogle · Software Engineer · Onsite · medium
- Compute shortest paths with blocked nodesGoogle · Software Engineer · Technical Screen · medium
Related concepts
- Graph Traversal And Shortest PathsCoding & Algorithms
- DFS/BFS Tree, Graph, And Grid TraversalCoding & Algorithms
- Shortest Path And Graph TraversalCoding & Algorithms
- BFS, DFS, Graph, And Grid TraversalCoding & Algorithms
- Graph Traversal, Shortest Paths, and Topological SortCoding & Algorithms
- Graph, Grid, BFS/DFS, And Union-FindCoding & Algorithms