Report One Circular Dependency Chain in a Build Target Graph

Last updated: November 29, 2026

Quick Overview

Given build targets and their declared dependencies, return one dependency cycle as an ordered list of targets (for example a -> b -> c -> a), or an empty list if the graph is acyclic.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given build targets and their declared dependencies, return one dependency cycle as an ordered list of targets (for example a -> b -> c -> a), or an empty list if the graph is acyclic.

An onsite question modeled on a Bazel-style build system, where an error message must show the actual cycle, not just say one exists. The interviewer wants three-color DFS and the bookkeeping to reconstruct the cycle path from the recursion stack.

What the Interviewer Expects
  • Use three-color DFS: unvisited, on stack, done
  • Detect a back edge to a node currently on the stack
  • Reconstruct the cycle from the stack or parent pointers
  • Make the DFS iterative to survive deep graphs
  • Handle targets that appear only as dependencies
Key Topics to Cover
cycle detection
three-color DFS
directed graph
back edge
build systems
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
  • Report all elementary cycles, and discuss why that can be exponential.
  • Return the smallest cycle.
  • How would you make the check incremental as BUILD files change?
  • Report strongly connected components instead of one cycle.
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: dict deps mapping a target to the list of targets it depends on.
  • Output: list like ['a', 'b', 'c', 'a'] or [].
  • Graph may have thousands of targets and deep chains....
High-Level Design

Topological sort (Kahn) tells you whether a cycle exists but not where. Three-color DFS does both. Each node is WHITE (unseen), GRAY (on the current DFS path), or BLACK (fully explored). An edge into ...


Submit Your Answer
Markdown supported