Skip to main content

22. Course Schedule

mediumAsked at Databricks

Detect a cycle in a directed prerequisite graph — the textbook DAG-validation problem that Databricks applies directly to detecting circular dependencies in Delta Live Tables pipeline DAGs.

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 course ai. Return true if you can finish all courses, false if there is a cycle.

Constraints

  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= 5000
  • prerequisites[i].length == 2
  • All prerequisite pairs are unique

Examples

Example 1

Input
numCourses = 2, prerequisites = [[1,0]]
Output
true

Explanation: Take course 0 then course 1 — no cycle.

Example 2

Input
numCourses = 2, prerequisites = [[1,0],[0,1]]
Output
false

Explanation: 0 depends on 1 and 1 depends on 0 — cycle exists.

Approaches

1. DFS cycle detection with color states

Mark nodes as unvisited (0), in-progress (1), or done (2). A back edge to an in-progress node reveals 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 state = new Array(numCourses).fill(0);

  function dfs(node) {
    if (state[node] === 1) return false; // cycle
    if (state[node] === 2) return true;  // already verified
    state[node] = 1;
    for (const neighbor of adj[node]) {
      if (!dfs(neighbor)) return false;
    }
    state[node] = 2;
    return true;
  }

  for (let i = 0; i < numCourses; i++) {
    if (!dfs(i)) return false;
  }
  return true;
}

Tradeoff:

2. Topological sort — Kahn's BFS

Compute in-degrees; enqueue nodes with in-degree 0; process the queue, decrementing neighbors' in-degrees. If processed count equals numCourses, the graph is acyclic.

Time
O(V + E)
Space
O(V + E)
function canFinish(numCourses, prerequisites) {
  const indegree = new Array(numCourses).fill(0);
  const adj = Array.from({ length: numCourses }, () => []);
  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 neighbor of adj[node]) {
      if (--indegree[neighbor] === 0) queue.push(neighbor);
    }
  }
  return processed === numCourses;
}

Tradeoff:

Databricks-specific tips

Databricks asks this because every DLT pipeline is a DAG; their runtime does topological scheduling before execution and rejects circular references with an error similar to what you're implementing here. Prefer Kahn's algorithm in your answer — it naturally produces an execution order (not just a yes/no) and interviewers often pivot to 'now return the order' as a follow-up. Mention that cycle detection is also the basis of deadlock detection in distributed lock managers.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Course Schedule and other Databricks interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →