The Course Schedule interview question is a classic dependency problem. You are given a total number of courses and a list of prerequisites where each prerequisite [a, b] means you must take course b before course a. You need to determine if it is possible to finish all courses. In graph terms, this is equivalent to checking if a directed graph contains a cycle.
This is one of the most popular graph interview patterns, asked by nearly every top-tier company including Amazon, Salesforce, and Microsoft. It evaluates your understanding of directed graphs and dependency resolution. It's a fundamental test of whether you can identify a problem as a cycle detection task and implement a robust traversal algorithm.
This problem is solved using Topological Sort, which can be implemented in two ways:
Courses: 2, Prerequisites: [[1, 0]] (Take 0 before 1)
0 -> 1.{0: 0, 1: 1}.Courses: 2, Prerequisites: [[1, 0], [0, 1]]
0 -> 1, 1 -> 0. (Cycle detected).{0: 1, 1: 1}.Always clarify if the graph is directed or undirected. Dependency problems are almost always Directed Acyclic Graphs (DAGs). Memorize Kahn's algorithm; it's often more intuitive to explain than the DFS states.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Course Schedule II | Medium | Solve | |
| Find Eventual Safe States | Medium | Solve | |
| Minimum Height Trees | Medium | Solve | |
| Course Schedule IV | Medium | Solve | |
| All Ancestors of a Node in a Directed Acyclic Graph | Medium | Solve |