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
Coding & Algorithms
Software Engineer
Thoughtworks
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
Topological sort
Cycle detection
DFS
Build systems
Monorepo
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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, ...


Submit Your Answer
Markdown supported

Related Questions