Graph Algorithms, Dependency Resolution And Connectivity
Asked of: Software Engineer
Last updated

What's being tested
These problems test graph modeling: converting prerequisites, build dependencies, grids, or flights into nodes and edges with the right direction and constraints. Interviewers are probing whether you can choose between topological sort, DFS cycle detection, Union-Find, and bounded shortest path instead of forcing one graph template everywhere.
Patterns & templates
-
Topological sort with
indegree+queue—O(V + E)time; if processed count< V, a directed cycle blocks completion. -
DFS cycle detection using
visiting/visitedstates —O(V + E); back-edge tovisitingmeans a dependency cycle exists. -
Build order dependency direction matters — for prerequisite
a -> b, decrementindegree[b]only afterais emitted. -
Union-Find / DSU for connectivity —
find,union, path compression, union by rank; near-constant amortized time, . -
Grid-to-graph mapping — convert
(r, c)toid = r * cols + c; union only valid land neighbors to count islands. -
Cheapest flight with K stops — use Bellman-Ford-style relaxation for
K + 1edges, or priority queue with(cost, node, stops)state. -
State includes constraints — shortest path with stop limits is not plain
Dijkstra; reaching a city cheaper but with more stops may be unusable.
Common pitfalls
Pitfall: Treating undirected connectivity logic as valid for directed dependency graphs; cycles and reachability have different meanings by edge direction.
Pitfall: Mutating the same distance array during bounded flight relaxation; use a copy per layer to avoid using too many edges.
Pitfall: Returning a partial build order without explicitly checking whether all nodes were processed.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Solve Knight and Reversal ProblemsUber · Software Engineer · Take-home Project · hard
- Find minimum activations to absorb all ballsUber · Software Engineer · Take-home Project · medium
- Count connected islands using Union-FindUber · Software Engineer · Take-home Project · medium
- Find minimum reversals to orient edges away from rootUber · Software Engineer · Take-home Project · medium
- Find shortest jumps in circular arrayUber · Software Engineer · Technical Screen · medium
- Return a Package Build OrderUber · Software Engineer · Technical Screen · medium
- Find earliest time all riders become connectedUber · Software Engineer · Onsite · hard
- Find cheapest flight with at most K stopsUber · Software Engineer · Technical Screen · hard
- Improve robustness of graph cycle detection codeUber · Software Engineer · Technical Screen · medium
- Detect cycle in directed dependency graphUber · Software Engineer · Technical Screen · medium
- Check feasibility of AI course scheduleUber · Software Engineer · Onsite · hard
- Compute build order from dependenciesUber · Software Engineer · Technical Screen · Medium
Related concepts
- Graph, Grid, And Connectivity AlgorithmsCoding & Algorithms
- Graph Traversal And Shortest PathsCoding & Algorithms
- Graph Algorithms For Relations And RoutingCoding & Algorithms
- Graph Search, Pathfinding, And ConnectivityCoding & Algorithms
- Tree And Graph ConnectivityCoding & Algorithms
- Graph, Grid, BFS/DFS, And Union-FindCoding & Algorithms