Q&A-1Explain modelling a problem as a graph and how it changes a production decision.
Answer
The 90-second answer
Modelling a problem as a graph is a decision about what a vertex is, and that decision is what changes the production call: usually from "which algorithm" to "can this be precomputed, or must it run per request."
A vertex is a state; an edge is a legal transition. The same physical objects produce different graphs depending on the question you ask, so you name one vertex in a sentence before choosing an algorithm. Once you have, the vertex and edge counts set the cost, and the cost is what decides the production question.
Worked example: service dependency graph. 400 services, 1,800 directed edges. The question is "if service X degrades, which services are affected?": reachability. BFS from one vertex is O(V+E) = 2,200 steps, microseconds. But you need it for every service, during an incident, while health checks run at 5k rps. Full transitive closure is 400 × 2,200 = 880,000 steps, roughly 1–3 ms in Go, and the result is 400 bitsets of 400 bits = 20 KB. That settles the production decision: precompute the closure on every deploy, store the bitsets, answer per request with a bitmask AND. Without the graph framing you write a recursive "walk the dependencies" function and meet its cost for the first time mid-incident.
The modelling choice is the trade-off.
| Model | Vertex | Edge | Cost | Reach for it when |
|---|---|---|---|---|
| Adjacency list | service | dependency | O(V+E) BFS, O(V+E) memory | Sparse; reachability, shortest path |
| Adjacency matrix | service | dependency | O(V²) memory, O(1) edge test | Dense; you test edges far more than you traverse |
| State-space | (position, budget) | one legal move | V = product of dimensions | The constraint is part of the state |
State-space is where the vertex count bites. "Fewest moves from A to B carrying at most 3 keys" on a 100×100 grid:
# Vertex is not the cell; it is (row, col, keys_held).
# 100x100 cells x 2^3 key subsets = 80,000 vertices.
start = (0, 0, frozenset())80,000 vertices is trivial. Add a second budget, say fuel up to 50, and it is 10,000 × 8 × 51 = 4.08M vertices, which changes the answer from "BFS per request" to "precompute or bound the budget." The vertex definition, not the algorithm, moved the decision.
When not to model as a graph. If the relation is a total order or a tree, a graph library is overhead: a sorted list or a tree walk is simpler and faster. If edges change per request, precomputation is invalid and you pay the traversal every time, then the decision is whether to cache, not which algorithm.
Failure modes. State-space explosion, as above, detected by logging |V| before you run. Assuming a DAG when the graph has cycles: a naive DFS recurses forever or a topological sort raises, detected by a cycle check at build time. Stale precomputation: the closure is computed on deploy, so a runtime dependency change (feature flag, dynamic routing) silently invalidates it. Detected by versioning the closure against the deploy and alerting on mismatch.
So the production decision is: define the vertex, count V and E, and let that count choose precompute versus per-request. Correctness first, then the cost model the count implies.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.