Graphs, Grids, And Reachability
Asked of: Software Engineer
Last updated

What's being tested
These problems test graph traversal, reachability, and choosing the right representation for directed, undirected, and grid graphs. You must distinguish BFS for unweighted shortest paths, DFS/Union-Find for connectivity, and state tracking for cycle detection, while stating time and space complexity clearly.
Patterns & templates
-
BFS shortest path — use a queue and distance map for unweighted graphs; runs in
O(V + E)time and returns minimum hop count. -
DFS reachability — recursively or iteratively explore neighbors with a
visitedset; avoid revisiting nodes, especially when graphs have cycles. -
Multi-source ancestor search — traverse parent links from each person, store all reachable ancestors, then intersect sets to test shared ancestry.
-
Directed meeting node — compute distances from both starts independently, scan nodes reachable from both, minimize
max(distA[i], distB[i]), then break ties by index. -
Grid traversal — model four-neighbor movement with direction vectors; mark cells visited immediately to prevent duplicate island counts.
-
Cycle detection — undirected graphs need parent tracking or Union-Find; directed graphs need three-state DFS: unvisited, active, and completed.
-
Obstacle handling — reject blocked start or destination, check bounds before indexing, and use BFS when every legal move has equal cost.
Common pitfalls
Pitfall: Marking a node visited only when dequeued can enqueue it repeatedly; mark it when discovered instead.
Pitfall: Treating directed edges as bidirectional changes reachability and can incorrectly identify paths, ancestors, or cycles.
Pitfall: Claiming
O(V + E)without definingVandE; for a grid, stateV = rows × columnsand explain memory usage.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Check Whether Two People Are Blood Relatives in a Family GraphAmazon · Software Engineer · Onsite · hard
- Minimum Time to Finish Dependent Cake-Making Steps When Steps Can Run in ParallelAmazon · Software Engineer · Technical Screen · medium
- Which Recipes Can Be Made When Ingredients Can Themselves Be RecipesAmazon · Software Engineer · Technical Screen · medium
- Fewest Moves for a Chess Piece With Type-Specific Step Directions to Capture the KingAmazon · Software Engineer · Technical Screen · medium
- Choose a Shared Reachable Node Minimizing the Worse DistanceAmazon · Software Engineer · Other · medium
- Find the Maximum Island AreaAmazon · Software Engineer · Online Assessment · medium
- Count Four-Neighbor Islands in a Binary MatrixAmazon · Software Engineer · Online Assessment · medium
- Solve two set and graph problemsAmazon · Software Engineer · Onsite · medium
- Find all cells reachable by downhill flowAmazon · Software Engineer · Technical Screen · easy
- Detect cycles in lists and graphsAmazon · Software Engineer · Technical Screen · medium
- Compute shortest path with obstaclesAmazon · Software Engineer · Technical Screen · medium
Related concepts
- Graph, Grid, BFS/DFS, And Union-FindCoding & Algorithms
- Graph, Grid, And Connectivity AlgorithmsCoding & Algorithms
- DFS/BFS Tree, Graph, And Grid TraversalCoding & Algorithms
- Graph Algorithms, Dependency Resolution And ConnectivityCoding & Algorithms
- Graphs, Grids, And Connected ComponentsCoding & Algorithms
- BFS, DFS, Graph, And Grid TraversalCoding & Algorithms