Back to Practice
#0253

Course Schedule

MediumDSA28 min20 XP

Problem

There are num_courses courses labeled 0 to num_courses - 1. Each prerequisite [a, b] means b must be taken before a. Return True if all courses can be finished.

Why This Matters

Topological sorting is how you reason about dependency graphs in courses, builds, workflows, and task schedulers.

Function Signature

def can_finish(num_courses, prerequisites):

Examples

Example 1
Inputnum_courses = 2, prerequisites = [[1, 0]]
OutputTrue

Take course 0 first, then course 1.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Simple dependency
Input2 courses, [[1, 0]]
ExpectedTrue

No cycle exists.

Cycle
Input2 courses, [[1,0],[0,1]]
ExpectedFalse

Each course waits for the other.

Hidden Test Categories
disconnected courseslong chainself prerequisite