Graph Algorithms For Relations And Routing
Asked of: Software Engineer
Last updated

What's being tested
You’re being tested on graph modeling: turning relations like similarity, exchange rates, or roads into nodes, edges, weights, and traversal rules. Interviewers look for correct algorithm choice, clean data structures, edge-case handling, and clear complexity reasoning.
Patterns & templates
-
Adjacency list with
`dict[node] -> list[(neighbor, weight)]`— best default for sparse graphs; build inO(E)space. -
DFS/BFS traversal for connected components or reachability —
O(V+E)time; trackvisitedbefore enqueueing to avoid cycles. -
Weighted path accumulation for ratios/conversions — multiply edge weights along a path; add reciprocal edges for bidirectional rates.
-
Dijkstra’s algorithm for nonnegative route costs —
O((V+E) log V)withheapq; don’t use plain BFS on weighted edges. -
Topological sort for DAG dependency chains —
O(V+E)using indegree queue; detect cycles when processed count< V. -
Union-Find for grouping linked records — near-constant amortized
find()/union(); useful when edges imply equivalence components. -
Consistency checking in weighted graphs — compare implied versus supplied ratios with epsilon tolerance; floating-point equality is unsafe.
Common pitfalls
Pitfall: Treating every graph as unweighted; bicycle routes and conversion rates require preserving edge costs or multiplicative weights.
Pitfall: Forgetting disconnected components; queries may involve two nodes with no valid path.
Pitfall: Overengineering dynamic updates; first solve the static graph correctly, then discuss incremental rebuilds or cached components.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Find linked user records by similarityStripe · Software Engineer · Technical Screen · medium
- Compute currency conversions with graph algorithmsStripe · Software Engineer · Onsite · Medium
- Compute currency conversions with graphs and topological sortStripe · Software Engineer · Onsite · Medium
- Plan bicycle routes on a city mapStripe · Software Engineer · Technical Screen · Medium
Related concepts
- Graph Algorithms, Dependency Resolution And ConnectivityCoding & Algorithms
- Graph Search And Weighted PathsCoding & Algorithms
- Tree And Graph Modeling AlgorithmsCoding & Algorithms
- Graph Search, Pathfinding, And ConnectivityCoding & Algorithms
- Graph Search, State Space, And Path OptimizationCoding & Algorithms
- Graph Versioning And Path DiscoveryCoding & Algorithms