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 true if you can finish all the courses. Return false if you cannot. You cannot finish them 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 = 3, prerequisites = [[1,0],[2,1]]
- Output:
- true
- Explanation:
- Take course 0, then course 1, then course 2.
Example 2
- Input:
- numCourses = 3, prerequisites = [[0,1],[1,2],[2,0]]
- Output:
- false
- Explanation:
- Course 0 needs course 1, course 1 needs course 2, and course 2 needs course 0. This is a cycle.
Constraints
- 1 <= numCourses <= 2000
- 0 <= prerequisites.length <= 5000
- prerequisites[i].length == 2
- 0 <= a, b < numCourses
- 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.
- Alien Dictionary
Coding · Array · Breadth-First Search · Depth-First Search
- Course Schedule II
Coding · Breadth-First Search · Depth-First Search · Graph Theory
- 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 · 6 records
Amazon
Leetcode 207. Course Schedule
Reported Oct 1, 2026 · 13 source reports
JUNIOR, MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceCloudflare
Leetcode 207. Course Schedule
Reported Feb 23, 2026 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceDoorDash
Leetcode 207. Course Schedule
Reported Feb 21, 2026 · 3 source reports
MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View sourceMerge
Leetcode 207. Course Schedule
Reported Jul 6, 2026 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View source