Order Build Targets So Every Dependency Builds First
Last updated: November 29, 2026
Quick Overview
There are n build targets labeled 0 to n-1 and a list of pairs (a, b) meaning target a depends on target b. Return any order that builds every target after its dependencies, or an empty list if a cycle makes it impossible.
Meta
November 29, 20260
0
0 solved
There are n build targets labeled 0 to n-1 and a list of pairs (a, b) meaning target a depends on target b. Return any order that builds every target after its dependencies, or an empty list if a cycle makes it impossible.
Meta's monorepo build tooling makes dependency ordering a natural framing for topological sort. Interviewers check that you build the graph in the right direction, detect cycles, and can explain both Kahn's algorithm and DFS coloring.
What the Interviewer Expects
- Builds the edge from dependency to dependent correctly
- Uses Kahn's algorithm with in-degrees, or DFS with three colors
- Detects cycles by checking the output length
- Handles targets with no dependencies
- States O(V + E) complexity
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the minimum number of parallel build rounds.
- Report one actual cycle to the user for debugging.
- Dependencies change incrementally; how do you avoid a full recompute?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
n up to 10^5, edges up to 2 * 10^5. Self-dependency counts as a cycle. Any valid order is acceptable. Output a list of length n or an empty list....
High-Level Design
Edge b -> a for each (a, b), so indegree[a] counts unmet dependencies. Start with every target of in-degree 0 in a queue. Pop a target, append it to the order, and decrement the in-degree of each de...