Talentd
FleetCode
Make and follow sheets.Sign in

Topological Sort Problems (38)

Problems tagged with Topological Sort. Start with the easy ones, then move to medium and hard.

0 Easy15 Medium23 Hard

Learn these first

1
Graph

Topological sort is defined on directed graphs. Understanding graph representations like adjacency lists and edge relationships is essential before applying the algorithm.

2
Queue

Queues are required for implementing Kahn's Algorithm, where nodes with zero in-degree are pushed and processed in order.

3
Depth-First Search

One standard method for topological sorting uses DFS with reverse postorder. Knowing DFS traversal, recursion, and visited states helps implement this approach correctly.

4
Dynamic Programming

Many advanced problems apply DP on a DAG after performing topological sorting, such as computing longest paths or counting paths.

5
Breadth-First Search

Kahn's Algorithm is essentially a BFS traversal on nodes with zero in-degree. Understanding BFS helps manage the processing order of nodes.

TitleSolutionAcceptDifficultyTopics
207. Course ScheduleVideo
Solution52.2%Medium
269. Alien DictionaryVideoPremium
Solution37.1%Hard
Array, Breadth-First Search +4· 18 companies
444. Sequence ReconstructionVideoPremium
Solution31.2%Medium
Array, Graph +1· 2 companies
631. Design Excel Sum FormulaVideoPremium
–39.3%Hard
Array, Design +3· 9 companies
851. Loud and RichVideo
Solution63.9%Medium
Array, Depth-First Search +2· 6 companies
913. Cat and MouseVideo
Solution36.2%Hard
1136. Parallel CoursesVideoPremium
Solution62.1%Medium
Graph, Topological Sort· 7 companies
1245. Tree DiameterVideoPremium
Solution61.1%Medium
Solution61.4%Hard
Array, Graph +2· 1 company
Solution42.7%Hard
Array, Graph +4· 3 companies
1728. Cat and Mouse IIVideo
Solution40.4%Hard
Array, Dynamic Programming +6· 1 company
Solution67.0%Hard
Array, Dynamic Programming +2· 9 companies
Solution56.8%Medium
Array, Graph +3· 10 companies
Solution51.1%Hard
Depth-First Search, Graph +1· 7 companies
Solution70.1%Hard
Array, Graph +4· 1 company
Solution79.5%Hard
Array, Graph +2· 1 company
Solution39.6%Hard
Array, Graph +2· 13 companies
–22.2%Hard
Array, Bit Manipulation +4· 1 company
3481. Apply SubstitutionsVideoPremium
Solution78.0%Medium
Array, Breadth-First Search +5· 1 company
Solution50.8%Hard
Array, Binary Search +5· 2 companies
Showing 1–38 of 38

More about Topological Sort

Topological Sort is a graph algorithm used to order the vertices of a directed graph such that for every directed edge (u → v), node u appears before v in the ordering. This ordering only exists for a Directed Acyclic Graph (DAG), which means the graph must not contain cycles. In coding interviews and real-world systems like task scheduling or dependency resolution, topological sorting helps determine the correct order to process tasks.

Topological Sort is a core concept in Graph algorithms and frequently appears in technical interviews at companies like Google, Amazon, and Meta. Many popular interview problems—such as course scheduling, dependency resolution, and build systems—are direct applications of this algorithm. Because of its strong connection with traversal techniques, mastering both Depth-First Search and Breadth-First Search is essential when learning topological sorting.

There are two primary approaches used to perform a topological sort:

  • Kahn's Algorithm (BFS approach) – Uses in-degree counts and a Queue to repeatedly process nodes with zero incoming edges.
  • DFS-based ordering – Performs DFS and records nodes in reverse postorder to produce a valid topological ordering.

Beyond simply ordering nodes, topological sorting is often combined with other techniques to solve more advanced problems. For example, dynamic programming on DAGs is a powerful pattern used for computing longest paths or counting paths in graphs, which relates closely to Dynamic Programming.

You should consider using topological sort whenever a problem involves dependencies, prerequisites, ordering constraints, or directed acyclic graphs. Common problem signals include phrases like "course prerequisites," "task dependencies," "build order," or "schedule tasks."

On FleetCode, you can practice 32 carefully selected Topological Sort problems that cover beginner to advanced patterns, helping you recognize DAG structures quickly and implement both BFS and DFS approaches confidently during interviews.

Topological Sort: common questions

What is Topological Sort in DSA?

Topological Sort is a graph algorithm that orders the vertices of a directed acyclic graph (DAG) so that for every edge u → v, node u appears before v. It is commonly implemented using either DFS with reverse postorder or Kahn's Algorithm with BFS.

What is the best way to learn Topological Sort?

Start by understanding DAGs and graph representations, then learn the two main algorithms: Kahn's BFS approach and the DFS reverse-postorder method. After that, practice dependency-based problems and DAG dynamic programming questions.

Is Topological Sort important for FAANG interviews?

Yes. Topological Sort frequently appears in FAANG and other big tech interviews because it models real-world dependency systems. Problems like course scheduling and build order are direct applications of this algorithm.

What are common patterns in Topological Sort problems?

Common patterns include dependency resolution, course scheduling, build order determination, cycle detection in directed graphs, and dynamic programming on DAGs. Recognizing prerequisite relationships is often the key signal to use topological sorting.

What are the best Topological Sort problems for interviews?

Classic interview problems include Course Schedule, Course Schedule II, Alien Dictionary, Minimum Height Trees, and Parallel Courses. These problems test your ability to detect cycles, process dependencies, and generate valid orderings.