Unique topological ordering
Given n items labeled 0..n-1 and a list of ordering constraints [a, b] meaning a must come before b, determine whether the constraints pin down exactly ONE valid total ordering of all items. Return True if a unique topological order exists, and False if there are zero valid orders (a cycle) or more than one. Duplicate constraints should be treated as a single constraint. There are at most 10^4 items.
Implement
unique_topo_order(n: int, edges: list[list[int]]) → boolExamples
in
[3,[[0,1],[1,2]]]outtrueWhat a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 30 min
solution.py
InputExpectedGot
[3,[[0,1],[1,2]]]truenot run yetsample