Lesson 7 of 70 6 min

Problem: Course Schedule (Topological Sort)

Master the logic of Directed Acyclic Graphs (DAG). Learn how to use Kahn’s algorithm or DFS to detect cycles in dependency systems.

Reading Mode

Hide the curriculum rail and keep the lesson centered for focused reading.

1. Problem Statement

Mental Model

Imposing order to reduce the search space complexity.

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

Return true if you can finish all courses. Otherwise, return false.

Input: numCourses = 2, prerequisites = [[1,0]]
Output: true (To take course 1 you should have finished course 0).

2. The Mental Model: The "Dependency Deadlock"

Imagine you are building a house. You cannot build the roof until the walls are up, and you cannot build the walls until the foundation is poured. These are Prerequisites.

A set of tasks and their dependencies can be modeled as a Directed Graph.

  • Nodes: Courses.
  • Directed Edge (A $\rightarrow$ B): Task A must be completed before Task B.

The only way you cannot finish the courses is if there is a Cycle (e.g., A needs B, B needs C, and C needs A). In a cycle, every task is waiting for another, creating a deadlock. Determining if a graph is a Directed Acyclic Graph (DAG) is the goal.

3. Visual Execution (Kahn's BFS logic)

graph TD
    0((0)) --> 1((1))
    1 --> 2((2))
    2 --> 0
    
    subgraph "Cycle Detected"
        Deadlock[In-degree never hits zero!]
    end

4. Java Implementation (Optimal O(V+E))

We'll use Kahn's Algorithm (BFS) based on In-degrees.

public boolean canFinish(int numCourses, int[][] prerequisites) {
    // 1. Build Adjacency List and In-degree array
    List<List<Integer>> adj = new ArrayList<>();
    int[] inDegree = new int[numCourses];
    for (int i = 0; i < numCourses; i++) adj.add(new ArrayList<>());
    
    for (int[] p : prerequisites) {
        adj.get(p[1]).add(p[0]); // p[1] -> p[0]
        inDegree[p[0]]++;
    }
    
    // 2. Add all courses with 0 prerequisites to the queue
    Queue<Integer> queue = new LinkedList<>();
    for (int i = 0; i < numCourses; i++) {
        if (inDegree[i] == 0) queue.offer(i);
    }
    
    // 3. Process the queue
    int count = 0;
    while (!queue.isEmpty()) {
        int curr = queue.poll();
        count++; // This course is finished
        
        for (int neighbor : adj.get(curr)) {
            inDegree[neighbor]--; // One prerequisite met
            if (inDegree[neighbor] == 0) {
                queue.offer(neighbor);
            }
        }
    }
    
    // 4. If finished count == total courses, no cycle existed
    return count == numCourses;
}

5. Verbal Interview Script (Staff Tier)

Interviewer: "Why is Kahn's algorithm preferable over a simple DFS for this problem?"

You: "While both algorithms can detect cycles in $O(V+E)$ time, Kahn's algorithm is more intuitive for Topological Order extraction. It mimics real-world parallel processing: courses with an in-degree of 0 are 'Ready to Start.' As we process them, we effectively satisfy dependencies for their neighbors. If at any point we have unvisited nodes but our queue is empty, we know a cycle exists because those nodes are mutually waiting on each other. Furthermore, Kahn's is an iterative BFS approach, which is safer for huge dependency graphs in Java because it avoids the stack depth limits inherent in recursive DFS."

6. Staff-Level Follow-Ups

Follow-up 1: "How do you return the actual order of courses?"

  • The Answer: "This is the 'Course Schedule II' problem. I would simply maintain a list of nodes as I poll them from the queue. If count == numCourses, that list is a valid topological ordering. If not, I'd return an empty array."

Follow-up 2: "How would you solve this in a distributed system (e.g., thousands of tasks across multiple servers)?"

  • The Answer: "For massive-scale task scheduling (like Airflow or Jenkins pipelines), I would use a Distributed Message Queue. Tasks with zero dependencies are published to the queue. Workers pick them up, execute them, and upon completion, update a metadata store (like Redis) that decrements the in-degree of downstream tasks. When an in-degree reaches zero, that task is then pushed to the queue."

7. Performance Nuances (The Java Perspective)

  1. Map vs. Array: Using an array of Lists List<Integer>[] is faster than a HashMap<Integer, List<Integer>> because primitive array access is $O(1)$ with no hashing overhead. In Java, remember to initialize each element of the array to a new ArrayList.
  2. Memory Management: For very large numCourses, consider using a flat int[] for the adjacency list (using a CSR - Compressed Sparse Row format) to minimize object allocation overhead and improve cache locality.

6. Staff-Level Verbal Masterclass (Communication)

Interviewer: "How would you defend this specific implementation in a production review?"

You: "In a mission-critical environment, I prioritize the Big-O efficiency of the primary data path, but I also focus on the Predictability of the system. In this implementation, I chose a recursive approach with memoization. While a recursive solution is more readable, I would strictly monitor the stack depth. If this were to handle skewed inputs, I would immediately transition to an explicit stack on the heap to avoid a StackOverflowError. From a memory perspective, I leverage primitive arrays to ensure that we minimize the garbage collection pauses (Stop-the-world) that typically plague high-throughput Java applications."

7. Global Scale & Distributed Pivot

When a problem like this is moved from a single machine to a global distributed architecture, the constraints change fundamentally.

  1. Data Partitioning: We would shard the input space using Consistent Hashing. This ensures that even if our dataset grows to petabytes, any single query only hits a small subset of our cluster, maintaining logarithmic lookup times.
  2. State Consistency: For problems involving state updates (like DP or Caching), we would use a Distributed Consensus protocol like Raft or Paxos to ensure that all replicas agree on the final state, even in the event of a network partition (The P in CAP theorem).

8. Performance Nuances (The Staff Perspective)

  1. Cache Locality: Accessing a 2D matrix in row-major order (reading [i][j] then [i][j+1]) is significantly faster than column-major order in modern CPUs due to L1/L2 cache pre-fetching. I always structure my loops to align with how the memory is physically laid out.
  2. Autoboxing and Generics: In Java, using List<Integer> instead of int[] can be 3x slower due to the overhead of object headers and constant wrapping. For the most performance-sensitive sections of this algorithm, I advocate for primitive specialized structures.

Key Takeaways

  • Nodes: Courses.
  • ****Directed Edge (A $\rightarrow$ B): Task A must be completed before Task B.
  • The Answer: "This is the 'Course Schedule II' problem. I would simply maintain a list of nodes as I poll them from the queue. If count == numCourses, that list is a valid topological ordering. If not, I'd return an empty array."

Want to track your progress?

Sign in to save your progress, track completed lessons, and pick up where you left off.