Course Schedule
There are numCourses courses numbered from:
0 to numCourses - 1
You are given prerequisites where:
[a, b]
means:
To take course a, you must first complete course b.
Determine whether it is possible to finish all courses.
Main Idea
This is a Directed Graph + Cycle Detection problem.
For example:
0 → 1
means:
Course 1 → Course 0
because course 1 must be completed before course 0.
If the graph contains a cycle, completing all courses is impossible.
Example
Suppose:
numCourses = 2
prerequisites = [[1,0]]
The dependency is:
0 → 1
There is no cycle.
Therefore:
true
But:
prerequisites = [[1,0],[0,1]]
creates:
0 → 1
↑ ↓
└───┘
There is a cycle, so:
false
Example 1
Input
2 1 1 0
Output
true
Explanation
There are two courses:
0
1
The prerequisite:
[1,0]
means:
0 → 1
We can complete:
Course 0
↓
Course 1
There is no cycle, so all courses can be completed.
Example 2
Input
2 2 1 0 0 1
Output
false
Explanation
The prerequisites create:
0 → 1
↑ ↓
└───┘
To complete 1, we need 0.
But to complete 0, we need 1.
This is a cycle.
Therefore, it is impossible to finish all courses.
Constraints
Hints:
Hint 1
Represent the prerequisites as a directed graph.
Hint 2
The problem becomes:
Does this directed graph contain a cycle?
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 16, 2026
Last updated
Aug 16, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output