Skip to content
Coding · Medium · Graphs · Target O(V + E) · Senior

Service dependency deploy order

Given service → dependencies, produce a deterministic deploy order or report the cycle that makes one impossible.

Statement

deps maps each service to the services it depends on. Return a deploy order in which every service appears after its dependencies. When several services are deployable, pick them in alphabetical order so the output is stable.

If there is a cycle, raise an error whose message names the services in the cycle.

Examples

{"api": ["db", "cache"], "worker": ["db"], "db": [], "cache": []}
→ ["cache", "db", "api", "worker"]

{"a": ["b"], "b": ["a"]}
→ ValueError("cycle: a → b → a")

Constraints

Up to 10⁴ services and 10⁵ edges. Dependencies may reference services that have no entry of their own.

Open to everyone until Pro goes on sale · sign in to save your attemptSolve it in the editor with visible tests
Open in the app