LC 207
LeetCode 207 · Medium

Course Schedule

Given prerequisite pairs, decide whether all courses can be finished.


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

Finishing all courses is possible iff the prerequisite graph is acyclic. Kahn's BFS peels off zero-in-degree nodes; finishing all of them means no cycle. DFS with a recursion-stack colour also works.

AspectValue
PatternTopological sort
Recognise it byCan a dependency DAG be ordered? Cycle detection.
Time complexityO(V+E)
Space complexityO(V+E)
DifficultyMedium

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.