Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Course Schedule

Problem in Plain English

Given a course count and prerequisite pairs, decide whether every course can be finished. The answer is yes exactly when the prerequisite graph has no directed cycle.

Problem Statement

There are numCourses courses labeled 0 to numCourses - 1, plus an array prerequisites where prerequisites[i] = [a, b] means course b must be taken before course a.

Return true if all courses can be finished and false otherwise; a dependency cycle makes finishing impossible.

Constraints

  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= 5000
  • prerequisites[i].length == 2
  • 0 <= a, b < numCourses
  • All prerequisite pairs are unique.

Examples

Example 1

Input
numCourses = 2, prerequisites = [[1,0]]
Output
true

Take course 0 first, then course 1, so every course finishes.

Example 2

Input
numCourses = 2, prerequisites = [[1,0],[0,1]]
Output
false

Course 1 needs course 0 while course 0 needs course 1, a cycle nobody can start.

Example 3

Input
numCourses = 1, prerequisites = []
Output
true

The single course has no prerequisite, so it can be taken immediately. An empty edge list does not mean there are no courses.

Example 4

Input
numCourses = 4, prerequisites = [[1,0],[0,1],[3,2]]
Output
false

Courses 2 then 3 can finish, but 0 and 1 require each other. Finishing one disconnected component is insufficient: every course must finish.

Example 5

Input
numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output
true

Finish 0, then 1 and 2 in either order, then 3. Course 3 becomes ready only after both of its prerequisites finish; a shared dependent is not a cycle.

Intuition

A prerequisite pair [1,0] is a directed edge from 0 to 1: course 0 unlocks course 1. Count how many prerequisites of each course are still unfinished. A zero count means the course is ready; finishing it removes one requirement from each dependent. Example 1 starts with counts [0,1], then taking 0 changes them to [0,0] so 1 can finish too. A cycle has no first course, so if ready courses run out with any course unfinished, the answer is false.

Approaches

Repeated Scan Kahn

Solution Details

Reveal Repeated Scan Kahn: intuition, complexity, and code

Hints

Hint 1
Read each pair [a, b] as a directed edge from b to a, since b must come first.
Hint 2
Think topological order: repeatedly take a course whose prerequisites are all done, if one exists.
Hint 3
If you place fewer courses than numCourses, the unplaced remainder holds the cycle, so answer false.

Edge Cases

  • No prerequisites means every course is immediately takable, so the answer is true.
  • A single course with no prerequisites finishes trivially.
  • A self-loop pair such as [0, 0] is a one-course cycle and must return false.
  • A cycle in one component forces false even if all other components can finish.
  • Several courses can become ready together; choosing any ready course preserves feasibility.
  • A 2000-course chain is valid and uses no recursion; 5000 unique prerequisite pairs test the edge-count bound.

Common Mistakes and Interview Tips

  • Enqueueing a dependent after just one prerequisite finishes instead of waiting for its count to reach zero. A diamond dependency needs both incoming edges removed.
  • Counting only courses present in prerequisite pairs misses isolated courses; initialize all numCourses courses.
  • Leaving out taken in repeated scanning picks the same zero-count course repeatedly and can falsely report success.
  • Returning true because some courses finished: a disconnected cycle still blocks the remaining courses.
  • Using JavaScript queue.shift repeatedly can make dequeues quadratic; the head cursor keeps the queue approach linear.
  • Storing a to b while counting incoming prerequisites for a mixes orientations. Use b to a for these Kahn implementations. Reversing every edge consistently preserves cycle existence, but reverses scheduling meaning.
  • Interview tip: explain why a nonempty remainder with no zero-indegree course must contain a directed cycle; a shared prerequisite or an undirected cycle alone does not establish one.

Key Takeaway

Cycle questions on directed graphs reduce to topological sorting: Kahn drains prerequisite-free nodes first, and anything left over proves a cycle.