Fewest Parallel Rounds to Roll Out Services With Dependencies

Last updated: November 29, 2026

Quick Overview

There are n services and dependency pairs [a, b] meaning a must be rolled out before b. In each round you may roll out any number of services whose dependencies are all done. Return the minimum number of rounds, or -1 if a cycle makes rollout impossible.

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

0

0

0 solved


There are n services and dependency pairs [a, b] meaning a must be rolled out before b. In each round you may roll out any number of services whose dependencies are all done. Return the minimum number of rounds, or -1 if a cycle makes rollout impossible.

A Meta onsite question set in the world of staged infrastructure rollouts, where independent services ship in parallel. The interviewer checks that you see the answer as the number of Kahn's algorithm layers (the longest chain in the DAG) and that you detect cycles with the same pass.

What the Interviewer Expects
  • Run Kahn's algorithm level by level, counting layers
  • Explain that the layer count equals the longest path in vertices
  • Detect a cycle when fewer than n services are processed
  • Keep time at O(n + e)
  • Contrast with a plain topological sort that loses the layering
Key Topics to Cover
Kahn's algorithm
BFS layers
longest path in DAG
cycle detection
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
  • What if each round can roll out at most k services?
  • How would you return which services go in each round?
  • How do you compute the answer if each service takes a different amount of time?
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: n services labeled 0..n-1, list of [a, b] edges.
  • Unlimited parallelism within a round.
  • Output: minimum rounds, or -1 if impossible....
High-Level Design

A service can go out in round r only after everything it depends on is out, so its earliest round is one more than the latest round among its prerequisites. Rolling out greedily as early as possible...


Submit Your Answer
Markdown supported