Recommended resources
The freely available resources this lesson will direct you to.
Hello! In our last lesson, we focused on problem recognition, learning to distinguish between scenarios requiring a Minimum Spanning Tree and those needing Dijkstra's shortest path algorithm. This skill of correctly modeling a problem is often more critical than memorizing code.
Today, we will continue this theme by exploring a new class of graph problems centered around order and dependencies. This lesson introduces Topological Sort, an algorithm designed for a special type of graph called a Directed Acyclic Graph (DAG). Our goal is to understand what these terms mean and, most importantly, to learn how to identify problems—from task scheduling to resolving software dependencies—that can be solved by finding a valid sequential ordering. This is a fundamental pattern that appears frequently in software engineering and technical interviews.
What is a Topological Sort?
Imagine you're creating a series of videos for a programming course. You can't release the video on "Dijkstra's Algorithm" before you've covered "Graphs" and "Priority Queues." This is a problem of dependencies: some tasks must be completed before others.
We can model this situation using a directed graph, where each node is a task (a video topic) and a directed edge from node A to node B means "A must be completed before B."

For an ordering to be possible, the graph must be acyclic—it cannot contain any cycles. If task A depends on B, and B depends on A, you have a circular dependency, and it's impossible to decide which to do first. A directed graph with no cycles is called a Directed Acyclic Graph, or DAG.
A topological sort (or topological ordering) of a DAG is a linear ordering of its nodes such that for every directed edge from node u to node v, u comes before v in the ordering. It's a valid sequence for performing the tasks.
The following video from WilliamFiset provides an excellent introduction to these concepts with clear, real-world examples.
Topological Sort Algorithm | Graph Theory
This video, "Topological Sort Algorithm," clearly explains the motivation for the algorithm and the properties of the graphs it applies to.
Please watch the first four minutes. The introduction gives several practical examples, like course prerequisites and program build dependencies, that will help you build intuition. The next section, from here, formally defines a topological ordering and, crucially, explains why it only exists for Directed Acyclic Graphs (DAGs).
It's important to realize that a topological ordering is often not unique. For the data processing graph above, one valid ordering is:Read -> Remove -> Summarize (Gender) -> Summarize (Location) -> Store -> Trigger (Gender) -> Trigger (Location)
Another equally valid ordering could be:Read -> Remove -> Summarize (Location) -> Summarize (Gender) -> Store -> Trigger (Location) -> Trigger (Gender)
The key is that all dependency arrows point "forward" in the sequence.

Identifying Problems that Require Topological Sort
The main challenge in a coding interview is recognizing that a problem can be modeled as a DAG and solved with topological sort. The problem description will rarely use these terms. Instead, it will describe a scenario involving prerequisites, dependencies, or a required order of events.
Your experience as a CS student and intern provides great context here. Think of a Makefile for compiling a C++ project or a package.json file in a Node.js application. Both define dependencies that must be resolved before a final target can be built or the application can run. These are real-world topological sort problems.
The following article from interviewing.io provides a great set of heuristics for spotting these problems.
Topological Sort Interview Questions & Tips
This section of the article gives you a practical checklist for identifying problems that are candidates for a topological sort solution.
Read the section titled When to Use. It boils down the pattern recognition to three key questions about dependencies, scheduling, and linear ordering.
To summarize and expand on these heuristics, you should think "topological sort" when the problem involves:
- Dependencies: A set of items (tasks, courses, files, packages) where some items have prerequisites. The goal is to find a valid order to process all of them.
- Example: "Given a list of courses and their prerequisites, return a valid order in which to take the courses." (LeetCode #207, #210)
- Scheduling: A sequence of jobs or events where constraints dictate the order.
- Example: "You are given a set of tasks and a list of pairs
(task_A, task_B)meaningtask_Amust be completed beforetask_B. Find a valid sequence of tasks."
- Example: "You are given a set of tasks and a list of pairs
- Inferring an Order: A set of relative ordering rules from which you must deduce a global, consistent order. The "Alien Dictionary" problem is a classic example, where you infer the alphabetical order of an alien language from a list of sorted words.
- Example: If you see
["xzy", "xyw"], you can infer thatzmust come beforewin the alien alphabet. By collecting all such pairwise rules, you build a dependency graph of characters and find its topological sort.
- Example: If you see
A Framework for the Interview
Once you suspect a problem requires topological sort, your ability to articulate the plan is as important as the code itself. Interviewers want to see that you can correctly model the problem and reason about its properties.
The article "Topological Sort & Dependency Resolution" from Hello Interview provides an excellent framework for how to approach this in an AI-assisted or traditional coding interview.
Topological Sort & Dependency Resolution in AI Coding Interviews
This resource offers a fantastic, practical guide on how to handle topological sort problems in an interview setting, focusing on pattern recognition and communication.
Please read two sections: Start with What interviewers expect. This section describes how to verbalize your thought process, from identifying the pattern to considering edge cases. Then, read the Quick reference checklist. This provides a concise mental model for tackling these problems step-by-step.
The key steps from this approach are:
- Identify the Pattern: State explicitly that the problem looks like a dependency graph and can be solved with topological sort. This shows you've correctly classified it.
- Model the Graph: Describe your graph representation. An adjacency list is almost always the best choice. You'll also need a way to track the "in-degree" of each node (the number of incoming edges), which is crucial for one of the main algorithms.
- Address Edge Cases: The most important edge case is the cycle. What happens if the dependencies form a loop (e.g., A requires B, B requires C, and C requires A)? In this case, no valid topological order exists. A complete solution must detect this and report it.
- Choose an Algorithm: Briefly mention your intended approach. In our next lessons, we'll cover the two primary methods: Kahn's algorithm (a BFS-based approach) and a DFS-based approach. For now, it's enough to know they exist.
Conclusion
In this lesson, we've laid the conceptual groundwork for topological sorting. You've learned to identify the underlying structure of dependency-based problems and how to frame them as finding a valid ordering in a Directed Acyclic Graph. This pattern-matching skill is invaluable for interviews and real-world problem-solving.
Key Takeaways:
- Directed Acyclic Graphs (DAGs): These are directed graphs with no cycles. They are the perfect structure for modeling anything with dependencies, from tasks to course prerequisites.
- Topological Sort: An algorithm that produces a linear ordering of the nodes in a DAG, respecting all dependencies. This ordering is often not unique.
- Problem Recognition: The key to solving these problems is to look for keywords like "dependencies," "prerequisites," "schedule," or "order." If the problem asks for a valid sequence, think "topological sort."
- Cycle Detection: A critical part of any topological sort problem is handling cycles. If a cycle exists, a valid ordering is impossible.
In our next lesson, we will move from what and when to how. We'll dive into the implementation of Kahn's algorithm, an intuitive, BFS-based method for generating a topological sort and detecting cycles.