Code RoomUnique topological ordering
MediumPrep Room Coding #1079

Unique topological ordering

CodingAlgorithms & data structuresMid–Senior~30 min

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]]) → bool
Examples
in[3,[[0,1],[1,2]]]outtrue
What 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
InputExpectedGot
[3,[[0,1],[1,2]]]truenot run yetsample