Graphs (Topological Sort)
Medium
Course Schedule
Determine if it is possible to finish all courses given prerequisites.
10 min read
Solve on LeetCodeProblem 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 == 0to Queue.Process Queue, decrementing neighbor in-degrees.
Count how many nodes we processed.
If
processed_count == numCourses, returntrue.
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.ON THIS PAGE
- Problem Understanding
- Strategy (Kahn's Algorithm)
- Interactive Visualization
- Dry Run
- Solution
- Edge Cases & Common Mistakes
- Complexity Analysis
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.
