Determine whether all courses can satisfy their prerequisites
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Overview: Determine whether a directed set of course prerequisites permits completion of every course. Handle cycles, disconnected components, duplicate requirements, and self-dependencies with a complete feasibility check.
Read the full Amazon Software Engineer interview experience this question came from
Constraints
- `1 <= num_courses <= 100000`.
- `0 <= len(prerequisites) <= 200000`.
- Every course identifier is an integer between 0 and `num_courses - 1`. Self-dependencies are allowed as invalid requirements.
Examples
Input: (3, [[1, 0], [2, 1]])
Expected Output: True
Explanation: The two prerequisites form a chain.
Input: (2, [[1, 0], [0, 1]])
Expected Output: False
Explanation: A two-course cycle prevents completion.
Hints
- A course can be taken when its count of unfinished prerequisites reaches zero.
- A disconnected component can contain a cycle even when other courses are feasible.