Problem
You must take numCourses courses. Each course has a number from 0 to numCourses - 1.
Each pair [a, b] in prerequisites means that you must finish course b before you start course a. Course b is a prerequisite of course a.
Return an order in which you can take all the courses. In this order, each course comes after all of its prerequisites. This kind of order is a topological order.
If more than one order is correct, return any of them. If no order is correct, return an empty list. No order is correct when the prerequisites make a cycle. A cycle is a chain of courses where each course needs the next course, and the last course needs the first course.
Example 1
- Input:
- numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
- Output:
- [0,1,2,3]
- Explanation:
- Course 0 comes first and course 3 comes last. Courses 1 and 2 can be in any order, so
[0, 2, 1, 3]is also correct.
Example 2
- Input:
- numCourses = 2, prerequisites = [[0,1],[1,0]]
- Output:
- []
- Explanation:
- Each course needs the other course first. This is a cycle, so no order is correct.
Example 3
- Input:
- numCourses = 3, prerequisites = []
- Output:
- [0,1,2]
- Explanation:
- No course has a prerequisite, so any order of the 3 courses is correct.
Constraints
- 1 <= numCourses <= 2000
- 0 <= prerequisites.length <= numCourses * (numCourses - 1)
- prerequisites[i].length == 2
- 0 <= a, b < numCourses
- a != b
- All the pairs in prerequisites are unique
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Course Schedule
Coding · Breadth-First Search · Depth-First Search · Directed Acyclic Graph
- Alien Dictionary
Coding · Array · Breadth-First Search · Depth-First Search
- Distance to a Cycle in Undirected Graph
Coding · Breadth-First Search · Depth-First Search · Graph Theory
- Longest Cycle in a Graph
Coding · Breadth-First Search · Depth-First Search · Graph Theory
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 11 records
Amazon
Leetcode 210. Course Schedule II
Reported Jul 28, 2025 · 2 source reports
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceApple
Leetcode 210. Course Schedule II
Reported May 16, 2026 · 2 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceDoorDash
Leetcode 210. Course Schedule II
Reported Nov 1, 2026 · 5 source reports
MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View sourceIntuit
Leetcode 210. Course Schedule II
Reported Dec 12, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source