Graphs (Topological Sort)
Medium

Course Schedule

Determine if it is possible to finish all courses given prerequisites.
Problem Understanding

There are numCourses courses labeled 0 to numCourses - 1. You are given prerequisites array where [a, b] means taking course b is mandatory before a.

Return true if you can finish all courses, false otherwise (i.e., if there is a cycle).

Strategy (Kahn's Algorithm)

This is a Cycle Detection problem in a Directed Graph.

  • Build Adjacency List and In-Degree array.

  • Push all nodes with in-degree == 0 to Queue.

  • Process Queue, decrementing neighbor in-degrees.

  • Count how many nodes we processed.

  • If processed_count == numCourses, return true.

Interactive Visualization
Step 1 / 1

Visualization coming soon...

Initializing...

1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.
Unlock VisualizerPREMIUM FEATURE

Stop Guessing, Start Mastering.

Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.

Start Your Premium Prep