KatInterview

Cycle-Safe Editing and Loop Limits for a Workflow Engine

System DesignModel answer
HardModel answerSystem Designsystem-designdagcycle-detection

Last updated

Design a service that blocks cyclic dependency edits, pins each run to a stable graph version, and caps retries and dynamic expansion so execution always terminates.

You are designing the control plane of a workflow orchestration platform. Users build pipelines as dependency graphs: an edge from A to B means A must finish before B starts. Users can edit these graphs at any time, and at runtime the engine supports conditional branches, dynamic step creation, and retries of failed steps.

Design the following:

  1. Topology validation. Every saved definition must be rejected if it contains a cycle. Explain the algorithm you would use for full-graph and single-edit validation, and what its cost depends on.
  2. Latency target. A stakeholder asks for "sub-millisecond validation." No maximum number of nodes, edges, or edits per request has been specified. Explain under which assumptions this could be met, and why it cannot be guaranteed for arbitrary submissions.
  3. Edits versus running executions. A run must always have a well-defined graph, even if someone edits the workflow while the run is in progress.
  4. Runtime loop protection. Retries and dynamic transitions must never produce an infinite execution. Describe which limits you would enforce, where their state lives, and how they survive scheduler restarts.

You do not need to invent a specific numeric limit or present benchmark results. Focus on reasoning, trade-offs, and failure modes. State the clarifying questions you would ask first.

Hints

Hint 1

A topological sort (Kahn's algorithm or DFS coloring) detects cycles in O(V + E). For a single new edge u -> v, you only need to ask whether v can already reach u.

Hint 2

Separate the editable draft from immutable published versions, and have every run store a reference to one version. Use optimistic concurrency (a version counter or compare-and-swap) so two concurrent edits cannot jointly create a cycle.

Hint 3

Model retries as an attempt counter on one node rather than as new graph edges, and give dynamic expansion its own budgets (nodes created, depth, total transitions, wall-clock deadline). Persist all counters transactionally with the state change they guard.