Code Room
CodingMediumcod-g511
Subject Topological sortLevel Mid–Senior~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.