The Parallel Courses problem asks you to find the minimum number of semesters to take all n courses, given prerequisite dependencies. In each semester, you can take all courses whose prerequisites have been completed. This Parallel Courses coding problem is equivalent to finding the longest path in a DAG (topological sort with level tracking). The graph and topological sort interview pattern is the core.
Uber, Amazon, TikTok, and Google ask this because it's a clean application of topological sorting to schedule optimization. The minimum semesters = the length of the longest dependency chain (critical path). If a cycle exists, return -1.
BFS-based topological sort with level tracking. Compute in-degrees. Start BFS from all courses with in-degree 0. Each BFS level = one semester. Count semesters until all courses are processed. If not all courses processed (cycle), return -1.
n=3, prerequisites=[(1,3),(2,3)]. Courses 1,2 have no prerequisites. Course 3 requires 1 and 2.
Parallel Courses is BFS topological sort where each level represents a time unit. The minimum semesters = BFS depth. The maximum courses you can take per semester is unlimited (unlike Parallel Courses II). Practice BFS topological sort until you can implement it fluently: initialize in-degrees, process level by level, detect cycles. This pattern appears in course scheduling, project planning, and dependency resolution.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| All Paths from Source Lead to Destination | Medium | Solve | |
| Sequence Reconstruction | Medium | Solve | |
| Maximum Employees to Be Invited to a Meeting | Hard | Solve | |
| Course Schedule IV | Medium | Solve | |
| Find Eventual Safe States | Medium | Solve |