24. Course Schedule
mediumAsked at BoxDetect a cycle in a directed prerequisite graph — Box solves the same problem when validating that enterprise workflow automation rules and folder-permission inheritance chains contain no circular dependencies.
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] indicates that you must take course bi before course ai. Return true if you can finish all courses, or false if there is a cycle making it impossible.
Constraints
1 <= numCourses <= 20000 <= prerequisites.length <= 5000prerequisites[i].length == 20 <= ai, bi < numCoursesAll prerequisite pairs are unique
Examples
Example 1
numCourses = 2, prerequisites = [[1,0]]trueExplanation: Take course 0 first, then course 1.
Example 2
numCourses = 2, prerequisites = [[1,0],[0,1]]falseExplanation: Courses 0 and 1 depend on each other — cycle detected.
Approaches
1. Brute force — DFS with recursion stack
Build adjacency list, then DFS from each unvisited node tracking a 'in current path' set. A back-edge into the current path signals 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);
const UNVISITED = 0, VISITING = 1, VISITED = 2;
const state = new Array(numCourses).fill(UNVISITED);
function dfs(node) {
if (state[node] === VISITING) return false; // cycle
if (state[node] === VISITED) return true;
state[node] = VISITING;
for (const nb of adj[node]) {
if (!dfs(nb)) return false;
}
state[node] = VISITED;
return true;
}
for (let i = 0; i < numCourses; i++) {
if (!dfs(i)) return false;
}
return true;
}Tradeoff:
2. Optimal — Kahn's algorithm (topological sort BFS)
Compute in-degrees; enqueue nodes with in-degree 0; each dequeue reduces neighbors' in-degrees. If all nodes are processed, 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) {
const node = queue.shift();
processed++;
for (const nb of adj[node]) {
inDegree[nb]--;
if (inDegree[nb] === 0) queue.push(nb);
}
}
return processed === numCourses;
}Tradeoff:
Box-specific tips
Box rates both DFS and Kahn's approaches equally — what separates candidates is the ability to articulate the real-world analogue. Mentioning that Box's permission-propagation engine must validate that group-inheritance graphs are acyclic before applying changes to millions of files shows you understand why the algorithm matters beyond the whiteboard.
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 Box interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →