Topological Sort LeetCode Problems: All 32, With Python Solutions
Every problem in this library that LeetCode tags Topological Sort — 32 in total, 24 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 32 problems
- 0 Easy
- 14 Medium
- 18 Hard
How Topological Sort problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Topological Sort problems — each one explains the approach, gives a Python template and states its complexity.
- Topological Sort — Order a set of tasks so that every dependency comes before the thing that needs it.
Topological Sort problems by difficulty
Problems with a complete Python solution are listed first, then by ascending problem number.
Medium (14)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 207 | Course Schedule | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 210 | Course Schedule II | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 310 | Minimum Height Trees | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 802 | Find Eventual Safe States | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 851 | Loud and Rich | Medium | Depth-First Search, Graph, Topological Sort +1 |
| 1462 | Course Schedule IV | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 1786 | Number of Restricted Paths From First to Last Node | Medium | Graph, Topological Sort, Dynamic Programming +2 |
| 1976 | Number of Ways to Arrive at Destination | Medium | Graph, Topological Sort, Dynamic Programming +1 |
| 2115 | Find All Possible Recipes from Given Supplies | Medium | Graph, Topological Sort, Array +2 |
| 2192 | All Ancestors of a Node in a Directed Acyclic Graph | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 444 | Sequence ReconstructionPremium | Medium | Graph, Topological Sort, Array |
| 1059 | All Paths from Source Lead to DestinationPremium | Medium | Graph, Topological Sort |
| 1136 | Parallel CoursesPremium | Medium | Graph, Topological Sort |
| 1245 | Tree DiameterPremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
Hard (18)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 329 | Longest Increasing Path in a Matrix | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 913 | Cat and Mouse | Hard | Graph, Topological Sort, Memoization +3 |
| 1203 | Sort Items by Groups Respecting Dependencies | Hard | Depth-First Search, Breadth-First Search, Graph +1 |
| 1632 | Rank Transform of a Matrix | Hard | Union Find, Graph, Topological Sort +3 |
| 1728 | Cat and Mouse II | Hard | Graph, Topological Sort, Memoization +5 |
| 1857 | Largest Color Value in a Directed Graph | Hard | Graph, Topological Sort, Memoization +3 |
| 1916 | Count Ways to Build Rooms in an Ant Colony | Hard | Tree, Graph, Topological Sort +3 |
| 2050 | Parallel Courses III | Hard | Graph, Topological Sort, Array +1 |
| 2127 | Maximum Employees to Be Invited to a Meeting | Hard | Depth-First Search, Graph, Topological Sort |
| 2246 | Longest Path With Different Adjacent Characters | Hard | Tree, Depth-First Search, Graph +3 |
| 2328 | Number of Increasing Paths in a Grid | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 2360 | Longest Cycle in a Graph | Hard | Depth-First Search, Breadth-First Search, Graph +1 |
| 2392 | Build a Matrix With Conditions | Hard | Graph, Topological Sort, Array +1 |
| 2603 | Collect Coins in a Tree | Hard | Tree, Graph, Topological Sort +1 |
| 269 | Alien DictionaryPremium | Hard | Depth-First Search, Breadth-First Search, Graph +3 |
| 631 | Design Excel Sum FormulaPremium | Hard | Graph, Design, Topological Sort +4 |
| 1591 | Strange Printer II | Hard | Graph, Topological Sort, Array +1 |
| 2371 | Minimize Maximum Value in a GridPremium | Hard | Union Find, Graph, Topological Sort +3 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
When the Topological Sort problem arrives live
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.