Recommended resources
The freely available resources this lesson will direct you to.
- Course Schedule II - Topological Sort - Leetcode 210www.youtube.com
- LeetCode 207 & 210: Course Schedule I & II | Topological Sort | Kahn's algorithm - Interview Ep 78www.youtube.com
- Topological Sort & Dependency Resolution in AI Coding Interviewswww.hellointerview.com
- Topological Sorting - Kahn's Algorithm - DSA Paniclewww.dsapanicle.com
Welcome back! In our last few lessons, we've built a solid foundation in topological sorting, covering both the BFS-based Kahn's algorithm and the recursive DFS-based approach. You now have two powerful tools for ordering nodes in a Directed Acyclic Graph (DAG).
Today, we shift our focus from implementation to application. Knowing how to perform a topological sort is only half the battle; the real skill in an interview setting is recognizing when to use it. This lesson is all about identifying those problem patterns. We'll explore how topological sort provides the essential framework for solving problems involving task scheduling, dependency resolution, and even finding the "critical path" in a project plan.
By the end of this lesson, you will be able to apply topological sort to solve a variety of problems that model dependencies and ordered tasks.
Recognizing the Topological Sort Pattern
The first step in applying any algorithm is recognizing that it's the right tool for the job. Problems requiring topological sort rarely say "use topological sort." Instead, they use a specific vocabulary.

Look for keywords and concepts that imply a directed, non-circular set of constraints:
- Prerequisites: Course A must be taken before Course B.
- Dependencies: Library X depends on Library Y.
- Scheduling: Task 1 must finish before Task 2 can start.
- Ordering: A sequence of steps must be performed in a specific order.
- Build Systems: A module must be compiled before another module that uses it.
When you see these phrases, your mental model should immediately be a Directed Acyclic Graph (DAG), where nodes are the items (tasks, courses, etc.) and edges represent the dependencies. The core problem then becomes finding a linear ordering that respects these edges, which is precisely the definition of a topological sort.
The following resource provides an excellent overview of these common variations and how to spot them.
Topological Sort & Dependency Resolution in AI Coding Interviews
This article section discusses the different disguises that topological sort problems wear in interviews.
Read the section on common variations. As you read, pay attention to the structure of each problem type: Parallel/Course Scheduling: How does the idea of "semesters" or "rounds" map to the algorithm? Alien Dictionary: What is the key step to perform before you can even run a topological sort? Longest Path in a DAG: Why is topological sort the enabling step for this calculation?
Now, let's dive into a few of these patterns with concrete examples.
Pattern 1: Scheduling with Parallelism
A common extension of the basic "Course Schedule" problem is to find the minimum time required to complete all tasks, assuming you can perform multiple tasks in parallel. Consider the "Parallel Courses" problem: given n courses and their prerequisites, and the ability to take any number of courses per semester, what is the minimum number of semesters needed?
This is where Kahn's algorithm (the BFS approach) shines. The core idea of Kahn's is to process nodes in "layers."
- The initial queue contains all nodes with an in-degree of 0. These are the courses with no prerequisites, which can all be taken in the first semester.
- After processing all of them, their neighbors' in-degrees are decremented. The nodes whose in-degrees become 0 are the courses that can be taken in the second semester.
- This continues layer by layer. The total number of layers (semesters) is the answer.
The number of semesters is simply the number of times we have to process the main while loop, where each iteration clears out the current "layer" of available courses from the queue.
Let's look at a practical implementation of this idea.
Topological Sorting - Kahn's Algorithm - DSA Panicle
This resource provides a solution to the "Parallel Courses" problem on LeetCode.
Find the section for "1136. Parallel Courses" and study the provided Python solution. Focus on how the algorithm is adapted from a standard Kahn's implementation: A semesters counter is incremented for each "level" of the BFS. A nested loop processes all nodes available in the queue at the start of a semester (for _ in range(m) where m = len(queue)). This is the key to processing level by level. How is a cycle detected? Notice the check if totalProcessed != n.
Pattern 2: Critical Path Analysis (Longest Path in a DAG)
In project management, a common task is to determine the minimum time to complete a project. This is known as the critical path method. A project is modeled as a DAG where nodes are tasks and have a duration (weight), and edges are dependencies. The "critical path" is the longest path through this DAG, and its length defines the total project duration.
Why is it the longest path and not the shortest? Because all tasks must be completed, the project's timeline is dictated by the longest chain of dependent tasks.
Finding the longest path in a general graph is NP-hard. However, in a DAG, it can be solved efficiently in time using topological sort. The sort gives us an ordering of tasks, guaranteeing that by the time we consider a task u, we have already computed the completion times for all its prerequisites.
The algorithm is as follows:
- Topologically sort the nodes of the graph.
- Initialize an array
earliest_finish_timefor all nodes to 0. - Iterate through the nodes
uin their topological order:- For each neighbor
vofu, update its earliest finish time:
earliest_finish_time[v] = max(earliest_finish_time[v], earliest_finish_time[u] + duration[u])
(Note: The logic can vary slightly based on whether weights are on nodes or edges. If weights/durations are on nodes, as shown here, we updatevbased onu's finish time. If weights are on the edges(u, v), we'd useduration(u, v)).
- For each neighbor
- The overall project completion time is the maximum value in the
earliest_finish_timearray after the loop finishes.

This pattern is extremely useful and connects directly to real-world scheduling, something you might encounter in a startup environment when planning development sprints.
Pattern 3: Implicit Graph Construction (Alien Dictionary)
Some of the trickiest topological sort problems are those where the graph isn't given to you explicitly. You must first deduce the nodes and edges from the problem's input. The "Alien Dictionary" problem is the canonical example.
Problem: You are given a list of words, sorted lexicographically according to the rules of a new, alien language. Your goal is to derive a valid ordering of the letters in this language.
Insight: The dependencies (edges) come from comparing adjacent words in the list.
- If you have the words
["wrt", "wrf"], they are identical up to the third character. The fact that"wrt"comes before"wrf"implies that in the alien alphabet,'t'must come before'f'. This gives you a directed edge:t -> f. - If you have
["z", "x"], you get the edgez -> x. - If you have
["za", "zb"], you get the edgea -> b.
By comparing every adjacent pair of words and finding the first differing character, you can build a graph where the nodes are characters and the edges are their precedence rules. Once you have this graph, you simply run a topological sort on it to find a valid alphabet ordering. Don't forget to handle cycle detection—if a cycle exists, the dictionary is inconsistent, and no valid ordering is possible.
Kahn's vs. DFS: A Practical Choice
In the previous lessons, you learned both Kahn's (BFS) and the DFS-based algorithms. Both run in time. So, how do you choose?
Topological Sort & Dependency Resolution in AI Coding Interviews
This reading provides practical advice on choosing between the two main topological sort algorithms in an interview context.
First, read the section comparing the two. Then, review the summary at the end: the key tradeoff.
As the article highlights, the choice is often practical:
- Kahn's (BFS) is often more intuitive for scheduling problems, especially those involving "levels" or parallelism (like "Parallel Courses"). Cycle detection is a simple
len(result) == num_nodescheck at the end. - DFS can be more natural if you're already thinking about the graph in terms of paths and recursion. It's compact to write, but cycle detection requires careful three-state tracking (unvisited, visiting, visited).
In an interview, either is fine as long as you can justify your choice and implement it correctly.
Conclusion
In this lesson, we elevated our understanding of topological sort from a mere algorithm to a powerful problem-solving pattern. You learned to identify its signature in problems involving scheduling and dependencies and explored its application in several key interview scenarios.
Key Takeaways:
- Pattern Recognition: Problems involving "prerequisites," "dependencies," or "ordering" are strong candidates for topological sort.
- Scheduling with Parallelism: Kahn's algorithm is a natural fit for finding the minimum number of "rounds" or "semesters," as its BFS nature processes nodes in layers.
- Critical Path: For DAGs with weighted nodes/edges, topological sort enables the efficient calculation of the longest path, crucial for project scheduling.
- Implicit Graphs: Be prepared for problems like "Alien Dictionary," where you must first construct the dependency graph from the input before you can sort it.
- Algorithm Choice: Kahn's is often more intuitive for level-based problems, while DFS is a solid choice if recursion fits the problem structure. Both are efficient and valid.
Topological sort is frequently the first step in a more complex problem. Recognizing it quickly allows you to structure the problem correctly and focus on the unique logic that follows.
This lesson concludes our deep dive into graph algorithms for now. In our next module, we will pivot to a completely new and fundamental paradigm in algorithm design: Dynamic Programming. We'll start by learning to identify its two key ingredients: optimal substructure and overlapping subproblems.