The Course Schedule IV interview question is a graph-based reachability problem. You are given a total number of courses and a list of direct prerequisites [a, b], which means course a is a prerequisite for course b. Additionally, you are given a list of queries [u, v], and for each query, you must determine if course u is a prerequisite of course v. A prerequisite can be direct or indirect (transitive).
Companies like Uber and Meta use the Course Schedule IV coding problem to test a candidate's understanding of graph reachability and transitive closure. It evaluates whether you can efficiently answer multiple queries about connections in a Directed Acyclic Graph (DAG). While a single reachability check is easy, answering many queries requires precomputation using algorithms like Floyd-Warshall or multiple BFS/DFS traversals.
This problem is solved using Transitive Closure logic. There are two main ways to implement this graph interview pattern:
isPre[i][j]. Initialize it with direct prerequisites. Then, use three nested loops to find all indirect prerequisites: isPre[i][j] = isPre[i][j] || (isPre[i][k] && isPre[k][j]).Courses: 3, Prerequisites: [[0, 1], [1, 2]]
Queries: [[0, 2], [2, 0]]
[0, 2]: Returns true.[2, 0]: Returns false (prerequisites only work in one direction).If the number of nodes is small (e.g., ), Floyd-Warshall is the easiest to implement. For larger , using a BFS for each node combined with bitsets can be more efficient.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find Eventual Safe States | Medium | Solve | |
| All Ancestors of a Node in a Directed Acyclic Graph | Medium | Solve | |
| Minimum Height Trees | Medium | Solve | |
| Course Schedule II | Medium | Solve | |
| Course Schedule | Medium | Solve |