LeetCode 210 ·
Medium
Course Schedule II
Return an order in which all courses can be taken, or empty if impossible.
Try it
Step through the core mechanic. The simulator below runs the topological sort shape this problem is built on.
Walk the pattern
No dedicated step-through for this one yet. The shape is Topological sort — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.
The approach
Kahn's algorithm: repeatedly emit a zero-in-degree node and decrement its neighbours. The emission order is a topological sort; if fewer than n nodes emit, a cycle exists.
| Aspect | Value |
|---|---|
| Pattern | Topological sort |
| Recognise it by | Produce a valid dependency order. |
| Time complexity | O(V+E) |
| Space complexity | O(V+E) |
| Difficulty | Medium |
Who asks it
Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.