🚀 Phases 1–5 are live — Days 1–17 cover the foundations and the algorithmic patterns. See the roadmap →
Day 23 - Topological SortPractice QuestionsOverview

Topological Sort Practice Questions

Ten interview classics, both algorithms represented. Each problem is a direct application of Kahn’s or DFS topological sort, or one of the standard variations from the applications page — once you spot the shape, the code writes itself.

Before reading any solution, force yourself through the four-question checklist:

  1. What’s the graph? (Nodes? Edges? Edge direction?)
  2. Which algorithm? (Kahn’s for “any/lex-smallest/rounds”; DFS for “report cycle / longest path”.)
  3. What’s the return value? (Boolean? List? Number?)
  4. What’s the cycle case? (Return false / empty / -1?)

The code is a 20-line transcription of those four answers. If you can answer them, you’ve solved the problem.

Easy

ProblemPatternStatus
Course ScheduleKahn’s count-check / cycle detectionAvailable
Find Eventual Safe StatesReverse-graph Kahn’s, or three-color DFSAvailable

Medium

ProblemPatternStatus
Course Schedule IIKahn’s, return the orderAvailable
Minimum Height TreesIterated leaf-stripping (Kahn’s on undirected graph)Available
Parallel CoursesKahn’s-with-roundsAvailable
Loud and RichDFS with memoization on a DAGAvailable
Sequence ReconstructionKahn’s with uniqueness checkAvailable
Longest Increasing Path in a MatrixDFS + memoization (a DAG induced on grid cells)Available

Hard

ProblemPatternStatus
Alien DictionaryBuild the precedence DAG from word pairs, then topoAvailable
Build a Matrix with ConditionsTwo independent topo sorts (rows, cols)Available

More Practice (Coming Soon)

ProblemPatternStatus
All Ancestors of a NodeBFS/DFS from each node on reversed graphComing Soon
Reconstruct ItineraryEulerian path (Hierholzer’s), not pure topoComing Soon
Course Schedule IVTransitive closure via topo + DPComing Soon
Minimum Number of Days to Eat N OrangesDP, not topo, but interesting cousinComing Soon
Find All Possible Recipes from Given SuppliesKahn’s on recipe graphComing Soon
Sort Items by Groups Respecting DependenciesTwo-layer topo sort (groups + items)Coming Soon
Number of Different Subsequences GCDsMath, not topo, but listed for completenessComing Soon
Strongly Connected Components (Tarjan / Kosaraju)DFS, generalization of cycle detectionComing Soon
Minimum Reorder to Make All Paths Lead to ZeroBFS on a tree, related “edge direction” topicComing Soon
Cracking the SafeEulerian path on de Bruijn graphComing Soon