19. Course Schedule
mediumAsked at GlassdoorGlassdoor models skill prerequisites and career-path dependencies as directed graphs — cycle detection via topological sort is the pattern they reach for when verifying those graphs are actually traversable.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
There are numCourses courses labeled 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] means you must take course bi before ai. Return true if you can finish all courses, false if a cycle makes it impossible.
Constraints
1 <= numCourses <= 20000 <= prerequisites.length <= 5000prerequisites[i].length == 2All prerequisite pairs are unique
Examples
Example 1
numCourses = 2, prerequisites = [[1,0]]trueExplanation: Take course 0 first, then course 1. No cycle.
Example 2
numCourses = 2, prerequisites = [[1,0],[0,1]]falseExplanation: Course 0 requires 1 and course 1 requires 0 — a cycle makes completion impossible.
Approaches
1. DFS cycle detection
Build an adjacency list and run DFS, tracking nodes in the current recursion stack. A back edge (visiting a gray node) indicates a cycle.
- Time
- O(V + E)
- Space
- O(V + E)
function canFinish(numCourses, prerequisites) {
const adj = Array.from({ length: numCourses }, () => []);
for (const [a, b] of prerequisites) adj[b].push(a);
// 0 = unvisited, 1 = in stack, 2 = done
const state = new Array(numCourses).fill(0);
function hasCycle(node) {
if (state[node] === 1) return true;
if (state[node] === 2) return false;
state[node] = 1;
for (const nb of adj[node]) {
if (hasCycle(nb)) return true;
}
state[node] = 2;
return false;
}
for (let i = 0; i < numCourses; i++) {
if (hasCycle(i)) return false;
}
return true;
}Tradeoff:
2. BFS Kahn's algorithm (topological sort)
Count in-degrees. Repeatedly enqueue nodes with in-degree 0 and decrement neighbors. If the total processed equals numCourses, no cycle exists.
- Time
- O(V + E)
- Space
- O(V + E)
function canFinish(numCourses, prerequisites) {
const adj = Array.from({ length: numCourses }, () => []);
const inDegree = new Array(numCourses).fill(0);
for (const [a, b] of prerequisites) {
adj[b].push(a);
inDegree[a]++;
}
const queue = [];
for (let i = 0; i < numCourses; i++) {
if (inDegree[i] === 0) queue.push(i);
}
let processed = 0;
while (queue.length > 0) {
const node = queue.shift();
processed++;
for (const nb of adj[node]) {
inDegree[nb]--;
if (inDegree[nb] === 0) queue.push(nb);
}
}
return processed === numCourses;
}Tradeoff:
Glassdoor-specific tips
Glassdoor's graph-heavy data model (companies → reviews → roles → skills) means engineers regularly reason about dependency chains. Interviewers want to hear you name the two approaches — DFS with coloring vs. Kahn's BFS — and know when each is cleaner. For interview settings, Kahn's iterative version is often easier to debug out loud. Verify your in-degree counting on a small example before claiming correctness.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Course Schedule and other Glassdoor interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →