SOARDO
Software Engineer (SDE)dsaintermediate3 interactive MCQs in app

Topological Sort

Kahn indegree-0 queue algorithmcycle detection via processed count < Vedge direction prereq -> dependent

Topological Sort orders every vertex of a Directed Acyclic Graph (DAG) so for every prerequisite edge `u -> v`, `u` appears BEFORE `v`. Kahn’s BFS algorithm computes `indegree[v]` for every node, pushes all `indegree == 0` roots into a `deque`, and pops each node `u`, decrementing `indegree[v]` of neighbors and pushing when `indegree[v]` hits `0`. If `len(order) < V`, a cycle makes valid ordering impossible!

for `0 -> 1`, `0 -> 2`, `1 -> 3`, `2 -> 3`, initial indegrees are `{0:0, 1:1, 2:1, 3:2}`; popping `0` drops `1` and `2` to `0`, then `3`, emitting valid topological order `[0, 1, 2, 3]`.
reversing edge direction (`course -> prereq` vs `prereq -> course`) or forgetting to verify `len(order) == V` to catch cycles.
Topological Sort — GeeksforGeeksread ↗Topological Sort — Applied Practicepractice ↗VisuAlgo Interactive Visualizationsvisualize ↗NeetCode DSA Roadmappractice ↗

Practice Topological Sort with 3 graded MCQs & written feedback.

Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.

Test yourself free on SOARDO →View full Software Engineer roadmap

More Software Engineer Study Guides