Browse and switch challenges
Topological Sort with Cycle Detection
Return a topological ordering of a directed acyclic graph, or an empty array if there is a cycle. When multiple nodes are eligible, choose the smallest index to ensure deterministic output.
Important
- -If a cycle exists, return
[]
. - -Nodes are
0..n-1
.
Signature
Constraints
- -
1 ≤ n ≤ 2e5
- -
|edges| ≤ 2e5
Examples
System prompt
0 tokens
Read-only
// Waiting for generated code…
// Your output will appear here as soon as the model returns JavaScript.
// It must define function toposort(…)
.
// Tip: write a prompt on the left and click "Generate code" .
0 tokens
Submission #1
0tokens(0/0)
Leaderboard
Loading leaderboard...