Code RoomLexicographically smallest task order
MediumPrep Room Coding #64

Lexicographically smallest task order

CodingAlgorithms & data structuresMid–Senior~30 min

You are given n tasks labeled 0..n-1 and a list of prerequisite pairs [a, b] meaning task b must be completed before task a. Return the lexicographically smallest valid ordering of all tasks (as a list). When multiple tasks are simultaneously available, always pick the one with the smallest label. If no valid ordering exists because of a cycle, return an empty list.

Implement
smallest_topo_order(n: int, prereqs: list[list[int]]) → list[int]
Examples
in[4,[[1,0],[2,0],[3,1],[3,2]]]out[0,1,2,3]
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
[4,[[1,0],[2,0],[3,1],[3,2]]][0,1,2,3]not run yetsample