mediumGraphsFacebookAmazonMicrosoft
Course Prerequisite Plan
A university registrar checks whether a student can finish every course given the prerequisite dependency graph.
There are n courses labeled 0..n-1 with prerequisite pairs [a, b] meaning you must take b before a. Return true if you can finish all courses.
Choose approach
0
1
2
3
queue
0
1/6
Narrationstep 1 of 6
Seed queue with zero-indegree nodes: [0].
Variables
queueSize=1
How to think about it
- Zero-indegree node = can be taken now.
- Removing it reduces its successors' indegrees.
- Cycle manifests as leftover indegrees > 0.
Edge cases to catch in interview
- No prereqs -> true.
- Self-loop -> false.
Examples
Example 1
false{"n":2,"prereqs":[[1,0],[0,1]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed