Check whether a prerequisite graph allows all courses to finish
Company: ByteDance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Check whether every course can be completed under directed prerequisites. Include disconnected components and repeated dependencies, and explain why an unprocessed cycle makes the overall schedule impossible.
Constraints
- `0 <= n <= 100000`.
- `0 <= len(prerequisites) <= 200000`.
- Each pair contains exactly two integers from `0` through `n - 1`.
- When `n == 0`, the prerequisite list is empty.
Examples
Input: (3, [[2, 1], [1, 0]])
Expected Output: True
Explanation: A simple chain has a valid completion order.
Input: (4, [[1, 0], [2, 3], [3, 2]])
Expected Output: False
Explanation: A detached cycle makes the full schedule impossible.
Hints
- Cycles must be detected in every disconnected part of the graph.
- Compare a prerequisite that is currently being explored with one whose entire subtree is already complete.