InterviewLegend

Topological Sort

Alex Chen30 min readMedium

You can't take Data Structures before Intro to Programming. That one prerequisite is a directed edge, and a surprising number of problems are just that edge multiplied: order the nodes so every one of them lands after the things it depends on. Topological sort hands you that order, or tells you it's impossible because a cycle means something eventually depends on itself. Course Schedule, build order, task scheduling, they're the same problem with the nouns swapped out. When a prompt says "prerequisites" or "dependencies," it's showing you a directed graph and asking for an order.

Topological Sort orders nodes so that for every directed edge u → v, node u comes before v in the ordering.

Only works on DAGs (Directed Acyclic Graphs). If there's a cycle, no valid ordering exists.
What's inside
  • 1 problem, worked step by step
  • about 30 min

Premium lessonData Structures & Algorithms

Keep going with Premium.

All 78 lessons and 103 worked problems in this course, plus what 100 companies ask, pulled from 20,000+ candidate reports.

100 companies. Checked every month.

Unlock the full course

$49 a year · less than $1 a week · Cancel anytime