Compute a Build Order and Report Cycles Across Service Modules
Last updated: November 29, 2026
Quick Overview
Given a map from each module in a monorepo to the modules it depends on, return a valid build order where dependencies build first, or if impossible, return one concrete dependency cycle so the team can break it.
Thoughtworks
November 29, 20260
0
0 solved
Given a map from each module in a monorepo to the modules it depends on, return a valid build order where dependencies build first, or if impossible, return one concrete dependency cycle so the team can break it.
Build tooling and modular architecture come up often on Thoughtworks engagements, where a consultant inherits a large client monorepo. The interviewer checks graph modelling, a correct topological sort, and whether the error output is actually useful to developers rather than just returning false.
What the Interviewer Expects
- Models modules as a directed graph and includes dependencies not listed as keys
- Uses three-colour DFS or Kahn's algorithm correctly
- Returns an actual cycle path, not only a boolean
- Produces deterministic output so CI logs are stable
- States complexity in terms of modules and edges
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
- How would you output build levels so independent modules build in parallel?
- Only one module changed. How do you find the minimal set to rebuild and test?
- How would you report all cycles, or the smallest set of edges to remove?
- How would you enforce no new cycles in CI?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: deps[m] is a list of modules that m depends on. Modules can appear only as dependencies. Output either (order, None) with each module after its dependencies, or (None, cycle) like `['a'...
High-Level Design
Run DFS with white, gray and black states. A node is appended to the order when all its dependencies are finished (post-order), which places dependencies first. Meeting a gray node means a back edge, ...