Build order for a package and its dependencies
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's technical phone screen, last asked Jan 2026. It tests topological sort, dfs and graphs.
You can look up a package's direct dependencies through an API. If package A depends on package B, then B must be built before A. Given a target package, return an order in which to build it and everything it transitively depends on, with every package after all of its dependencies. The full dependency graph is not given up front: discover only the part reachable from the target by calling the API.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
get_dependencies(package: str) -> list[str]— provided; may be slow. -
build_order(target: str) -> list[str]— any valid order of the target and its transitive dependencies, each listed once. Packages the target does not need are left out. If the reachable graph has a cycle, return[].
Example
dependencies = {"app": ["lib", "ui"], "lib": ["core"], "ui": ["core"], "core": [], "docs": ["core"]}
build_order("app") -> ["core", "lib", "ui", "app"] # core once; docs is not needed
build_order("core") -> ["core"]
dependencies = {"a": ["b"], "b": ["a"]}
build_order("a") -> [] # a cycle
Constraints
- Up to 10^5 reachable packages and 10^6 dependency edges.
- A dependency may be listed twice for the same package.
Clarifying questions
- Is any valid order acceptable, or must it be deterministic?
- On a cycle, return
[], raise, or report the cycle? - What if a dependency does not exist or the API call fails?
- Should each package's dependencies be fetched only once? Is the API expensive?
- Can the graph be deep enough to hit the recursion limit?
What a strong answer covers
- DFS from the target with three states (unvisited, in progress, done): append a package after all its dependencies are done, and report a cycle on reaching an in-progress package.
- Or Kahn's algorithm: first discover the reachable subgraph, then repeatedly build packages with no unbuilt dependencies; a leftover package means a cycle. The trade-offs between the two.
- Calling
get_dependenciesat most once per package and caching the result. - An iterative DFS for deep graphs; O(V + E) time and space over the reachable part.
- Tests: duplicates, shared dependencies, a self-dependency, a missing package.
Follow-ups
- Return the packages in levels that can be built in parallel.
- Report the cycle itself, such as
a -> b -> a. - The same logic orders service deployments where some services must start before others. What is different?
- Some packages are already built and cached. Skip them and their subtrees if possible.
Where it's asked
- Uber · Technical phone screen · Last asked Jan 202660 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design
What it tests
topological sort · dfs · graphs
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
