There are numCourses courses, numbered 0 to numCourses − 1. Each pair [a, b] in prerequisites says that course b has to be finished before you can start course a.
Return an order in which you can take every course while respecting all the pairs. Any correct order is accepted. If no such order exists, because some courses depend on each other in a loop, return an empty list.
Example 1
Input: numCourses = 4, prerequisites = [[3,2],[1,0],[2,1],[3,1],[2,0]] Output: [0, 1, 2, 3] Why: each course needs the one before it, so only one order works
2 more examples and the constraints
Example 2
Input: numCourses = 3, prerequisites = [[1,0],[2,1],[1,2]] Output: [] Why: 1 needs 2 and 2 needs 1, so neither can start
Example 3
Input: numCourses = 3, prerequisites = [] Output: [0, 1, 2] Why: nothing is required, so any order is fine
Constraints
- 1 ≤ numCourses ≤ 2,000
- 0 ≤ number of pairs ≤ numCourses × (numCourses − 1)
- In each pair, a and b are different valid courses
- No pair appears twice
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Repeated scansO(V · (V + E))
- DFS post-orderO(V + E)
- Kahn’s algorithm (BFS)O(V + E)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 2,000 courses in a chain | Verdict |
|---|---|---|---|
| Repeated scans | O(V · (V + E)) | Mention it, then move on | |
| DFS post-order | O(V + E) | Passes; watch the recursion depth | |
| Kahn’s algorithm (BFS) | O(V + E) | Best; no recursion, cycle check for free |
V = numCourses, E = number of prerequisite pairs.
Edge cases to test
- No prerequisites. Every course is ready at once; any order of 0 to numCourses − 1 is correct.
- A cycle. Courses that need each other never reach 0, so the order comes up short: return
[]. - A cycle in one part only. Even if most courses can be taken, a single loop anywhere means the answer is
[]. - Courses no pair mentions. They still belong in the order; start them at indegree 0.
- Long chain. 2,000 courses in a line: the DFS recursion goes 2,000 deep.
Common mistakes
- Drawing the arrow the wrong way round: [a, b] means b comes first, so the arrow is b → a.
- Returning the partial order when there is a cycle instead of an empty list.
- In the DFS, using one visited flag, which cannot tell a finished course from one on the current path.
How to talk through it
- 1Clarify. Does [a, b] mean b before a? Is any valid order fine? What should a cycle return?
- 2Give the brute force. Scan for a ready course, take it, repeat. Say why it can need V passes.
- 3Name the pattern. This is a topological sort of a directed graph. A cycle means no order exists.
- 4Code Kahn’s algorithm. Indegrees, a queue of zeros, lower the counts as you take courses. Check the order’s length at the end.
- 5Test out loud. Run Example 1 and the two-course loop, and say why the loop leaves the order short.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?