Course Schedule
Problem
There are numCourses courses labelled 0 through numCourses - 1. You are given a list prerequisites of pairs [a, b] meaning "you must complete course b before you can take course a". Decide whether it is possible to finish every course, i.e. whether some ordering of all courses respects every prerequisite.
- There are at most 3,000 courses and at most 8,000 prerequisite pairs.
- Each pair contains two distinct course labels; pairs are not repeated.
Examples
numCourses = 3, prerequisites = [[1, 0], [2, 1]]→true. A straight chain: take 0, then 1, then 2.numCourses = 4, prerequisites = [[1, 0], [2, 1], [3, 2], [0, 3]]→false. The chain wraps back around into a 4-course cycle.numCourses = 5, prerequisites = []→true. No constraints at all.
Hints
- Draw each prerequisite as a directed edge. What property of the graph makes finishing impossible?
- A course you can start right now is one with no unfinished prerequisites. After you finish it, which other courses become startable?
- Detect a cycle in a directed graph: either Kahn's algorithm (repeatedly remove nodes with in-degree 0 and check whether all nodes were removed) or DFS with white/gray/black coloring.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.