Course Schedule
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you must take course `bi` first if you want to take course `ai`. Return `true` if you can finish all courses. Otherwise, return `false`. **Examples:** ```typescript canFinish(2, [[1, 0]]) // true canFinish(2, [[1, 0], [0, 1]]) // false ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(V + E)
- Space complexity: O(V + E)
Kahn's algorithm (topological sort) visits each vertex V and edge E once.
Follow-Up Questions Interviewers Ask About Course Schedule
- How would you adapt this to return the actual ordering of courses?
Return the sequence of nodes added to the queue during BFS (which is Course Schedule II).
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.