Problem: Output a topological ordering
Given a directed graph with n nodes labeled 0..n-1 and a list of directed edges edges, return a topological ordering of the nodes.
A topological ordering is a permutation of all nodes such that for every directed edge (u, v), node u appears before node v in the ordering.
Input
-
Integer
n
-
List
edges
where each element is a pair
(u, v)
meaning
u -> v
Output
-
Return a list of length
n
containing one valid topological order.
-
If no topological order exists (graph contains a cycle), return an empty list (or explicitly signal failure).
Constraints (reasonable defaults)
-
1 <= n <= 2e5
-
0 <= |edges| <= 2e5
Follow-up
-
Explain how you would ensure the returned order is correctly produced (i.e., not just detecting that a topo sort exists, but actually outputting the sequence).