AI Interviews · Question

Detect and break cycles in a graph of pod dependencies.

Coding (DSA) question reported at Together AI (2026). Sources, confidence and an answer card.

Researched · updated

infra-102 AI Infra Engineer Classic DSA & object-oriented design Coding (DSA) Low confidence
On this page

What they’re really asking

If you only remember one thing, say this in the first minute: “Pods and their dependencies form a directed graph. I’ll find cycles with an iterative depth-first search that marks each node white, grey or black; reaching a grey node again closes a cycle. To break cycles, I remove the cheapest edge on each cycle I find and repeat. Removing the fewest edges overall is NP-hard, so my result has no cycles but isn’t guaranteed minimal. And for operators, the cycle itself, in order, is often the most useful output.”

The rest of this page explains each part of that answer.

A pod is the unit Kubernetes runs: one or more containers deployed together. Pods depend on each other: the API needs the database, the web front end needs the API. Write each “A needs B” as an arrow from A to B and you have a directed graph. A valid start order exists only if the graph has no cycle, a path of arrows that comes back to where it started. If the API needs auth and auth needs the API, neither can start first.

Detecting a cycle is a standard algorithm. What makes this question different is the second half, “break them”. Breaking means deleting dependencies, and that raises two questions an algorithm can’t answer alone: which dependency is allowed to go, and is the smallest possible fix even practical to compute? A good answer handles both, and says what an operator would actually want to see.

(PracHub lists a Together AI Software Engineer question titled “Detect cycles and break them in pod dependencies”, dated Feb 12, 2026, marked medium PracHubaggregator · 2026. Only the title is reported, so the input format and the meaning of “break” are yours to clarify.)

Words you need first

  • Edge: one arrow, “A depends on B”.
  • Three-state DFS: depth-first search that colours each node white (not visited yet), grey (on the path being explored right now) or black (finished, with everything below it). Reaching a grey node means you have come back to your own path: a cycle.
  • Topological order: an order of the nodes where every arrow points the same way, so dependencies can be started first. It exists only when there is no cycle.
  • Kahn’s algorithm: builds a topological order by repeatedly taking a node that no remaining arrow points to.
  • Minimum feedback arc set: the smallest set of edges whose removal leaves no cycles.

Clarify first

  • Graph: pod → pods it depends on? Directed? How many nodes and edges?
  • Output: a yes/no, one cycle, every cycle, or a repaired graph?
  • Breaking: which edges may be removed? Is there a cost (optional versus required dependency)? Must the removal be minimal?
  • After breaking: do they want a start order (a topological sort) too?

Say it like this: “Are some dependencies soft, so I can give each edge a cost and remove the cheapest one?”

Answer

The example to follow

Six pods. auth needs api only for an optional health check, so that edge is cheap to remove (cost 1). api truly needs auth (cost 10). Every other edge costs 5.

   web ──────────► cache
    │                ▲
    ▼                │
   api ──────────────┘
    │  ▲
    │  │ (optional, cost 1)
    ▼  │
   auth

   api ──► db ◄── worker

One box at a time:

  • web needs api and cache.
  • api needs auth, cache and db.
  • auth needs api, but only optionally. This edge and api → auth form the cycle.
  • cache and db need nothing.
  • worker needs db.

Notice the diamond: web reaches cache directly and also through api. That is not a cycle, and a detector that uses only two states (visited or not) wrongly reports it as one. That is why you need grey and black.

Detect, then break

Detect with an iterative three-state DFS: reaching a grey node closes a cycle, and the current path gives it. “Iterative” means an explicit stack instead of recursion, because Python’s default recursion limit is about a thousand frames and a long chain of dependencies would crash. Break greedily: remove the cheapest edge on the found cycle, and repeat until none is left.

def break_cycles(graph, weight=lambda u, v: 1):
    """Remove the cheapest edge of each cycle found until none is left. Greedy, not minimal."""
    g = {u: list(vs) for u, vs in graph.items()}
    removed = []
    while (cyc := find_cycle(g)) is not None:      # three-state DFS; returns [a, b, ..., a]
        u, v = min(zip(cyc, cyc[1:]), key=lambda e: weight(*e))
        g[u].remove(v)
        removed.append((u, v))
    return g, removed

find_cycle is on the eng-115 card. Each round is O(V + E) (V nodes, E edges), and there are at most E rounds.

Run on the example (2026-10-12), it goes like this:

StepResultWhat to say in the interview
Detectfind_cycle returns [api, auth, api]”Here’s the cycle, in order.”
Pick an edgeapi → auth costs 10, auth → api costs 1”Remove the cheapest edge on the cycle.”
Removeremoved = [(auth, api)]”The optional health-check dependency goes.”
Detect againno cycle”The graph is now acyclic.”
OrderKahn’s algorithm places all six pods”Here’s a valid start order.”

Then Kahn’s algorithm gives a start order. Any nodes it can’t place were on a cycle or behind one, which doubles as a second detection method. On the original graph, Kahn’s places only web and worker; api and auth are on the cycle, and cache and db are stuck behind it. After the repair it places all six. Because the arrows point from a pod to what it needs, start the pods in the reverse of Kahn’s order: db, cache, auth, api, worker, web.

The statement to make

Removing the fewest edges so the graph has no cycle (the minimum feedback arc set) is NP-hard: no known algorithm does it quickly for every graph. So the greedy result is acyclic but not guaranteed minimal. Removing every back edge (an edge into a grey node) from one DFS is another quick valid answer.

In tests run for this page on 2026-10-12, the greedy repair always left a graph that Kahn’s algorithm could fully order, on 500 random graphs, and DFS detection agreed with Kahn’s on whether a cycle existed.

Say it like this: “Detecting a cycle is linear. Breaking all cycles with the fewest removed edges is NP-hard, so I’ll do it greedily and tell you what that costs.”

What an operator actually wants

For an operator, the most useful output is often the cycle itself, in order, so a person can decide which dependency was a mistake. Propose returning that alongside the repair. In the example, “api → auth → api” tells someone exactly which two services to look at. A silently deleted edge tells them nothing.

The arithmetic behind the numbers (for a Staff-level follow-up)
  • One detection: each node is pushed and popped once and each edge followed once: O(V + E).
  • Greedy breaking: every round removes one edge, so at most E rounds: O(E × (V + E)) in the worst case. In practice the number of rounds is the number of cycles you break, which is usually small.
  • Kahn’s algorithm: O(V + E), and the leftover nodes (nonzero in-degree at the end) are on a cycle or downstream of one.

Senior vs Staff

Pod dependency cycles

Senior

  • Iterative three-state DFS that returns the cycle, greedy breaking with a cost, Kahn's order after.

Staff

  • States the NP-hardness of minimal breaking, asks what an edge's cost means operationally, and argues for reporting cycles to a human rather than silently removing dependencies.

Small-team version

A team’s deploy or job graph needs a cycle check at write time with a readable error, and that is all. Your orchestrator or CI system almost certainly has one already.

What to do:

  • Fail the change and show the cycle. Silently removing a dependency in production is rarely what anyone wants.
  • Build only the check, if you need one. A three-state DFS check in CI is an hour or two of work, about $93–186, against weeks for a home-grown DAG runner (a DAG, directed acyclic graph, is a dependency graph with no cycles).
  • Revisit only if you outgrow the engine’s own validation.
The arithmetic behind the numbers (for a Staff-level follow-up)

A loaded engineer-hour of about $93 comes from the BLS median developer pay of $135,980 (May 2025) ÷ 0.70 (wages are about 70% of total compensation) ÷ 2,080 hours a year. One to two hours is about $93–186.

Pitfalls

  • A two-colour visited set. It calls a diamond, like web → api → cache plus web → cache, a cycle.
  • Recursive DFS. It overflows on long dependency chains in Python.
  • Claiming the greedy repair is minimal. It is acyclic, not minimal; the minimal version is NP-hard.
  • Removing an arbitrary edge without asking what dependencies mean. Some edges are optional; most are not.

Go deeper

Family

Short DSA screens with an infrastructure flavor: idle GPUs, padding, scheduling, cycles

Classic algorithm problems dressed in infrastructure words: idle GPUs, padding uneven arrays, scheduling jobs, finding loops. Name the pattern, ask the one question that changes the algorithm, and test against a brute force.

Other questions in this family