Graph Search, State Space, And Path Optimization
Asked of: Software Engineer
Last updated

What's being tested
These problems test graph traversal, state-space modeling, and path optimization under constraints. You need to choose between DFS + memoization, BFS over encoded states, and DAG dynamic programming, then justify correctness, reconstruction, and O(V + E)-style complexity.
Patterns & templates
-
DFS + memoization for longest decreasing grid paths —
dfs(r,c)returns best suffix;O(R*C)states, avoid revisiting recomputation. -
Topological DP on DAGs — process nodes in topo order; update
dp[v] = max(dp[v], dp[u] + reward - cost). -
BFS over state space — encode
(row, col, keysMask)for shortest key collection; first time reaching full mask is optimal. -
Bitmask state encoding — map keys to bits using
1 << k; visited becomesvisited[r][c][mask], oftenO(R*C*2^K). -
Path reconstruction — store
parent[state] = prevStateornext[node]; reconstruct after DP/BFS without polluting scoring logic. -
Grid graph idiom — use
dirs = [(1,0),(-1,0),(0,1),(0,-1)]; bounds-check before height, wall, lock, or visited checks. -
Score vs distance distinction — BFS minimizes unweighted steps; weighted DAG maximization needs DP, not greedy local best or plain BFS.
Common pitfalls
Pitfall: Treating
(r, c)as visited in key-collection problems is wrong; the same cell with different keys is a different state.
Pitfall: Using DFS without memoization on decreasing-path grids can explode exponentially despite the grid having only
R*Cmeaningful states.
Pitfall: Forgetting to prove acyclicity before DAG DP; if cycles exist, longest/max-score path may be undefined or require different algorithms.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Compute maze score using shortest pathAirbnb · Software Engineer · Onsite · hard
- Find best downhill ski run from a startAirbnb · Software Engineer · Onsite · hard
- Find max-score path in weighted DAGAirbnb · Software Engineer · Technical Screen · Medium
- Maximize tokens from nested cratesAirbnb · Software Engineer · Onsite · Medium
- Solve palindrome pairs and key pathAirbnb · Software Engineer · Onsite · Medium
- Maximize path score in DAGAirbnb · Software Engineer · Technical Screen · Medium
- Compute shortest path to collect all keysAirbnb · Software Engineer · Onsite · Medium
Related concepts
- Graph Search And Weighted PathsCoding & Algorithms
- Graph Search, Pathfinding, And ConnectivityCoding & Algorithms
- Graph, Grid, BFS/DFS, And Union-FindCoding & Algorithms
- DFS/BFS Tree, Graph, And Grid TraversalCoding & Algorithms
- Graph Algorithms, Dependency Resolution And ConnectivityCoding & Algorithms
- Graph Traversal, Shortest Paths, and Topological SortCoding & Algorithms