Code RoomLexicographically smallest tournament ranking
HardPrep Room Coding #1458

Lexicographically smallest tournament ranking

CodingAlgorithms & data structuresSenior–Staff~40 min

A tournament is a directed graph on n players (0..n-1) where, for every unordered pair {i, j}, exactly one directed edge exists; matches is the list of those edges, where [u, v] means u beat v. A full ranking is an ordering of all players where each player beat the next one in the ordering (a Hamiltonian path along the beat edges) -- one always exists in a tournament. Return the lexicographically smallest such ordering as a list of player ids. Constraints: 2 <= n <= 20.

Implement
lex_ranking(n: int, matches: list[list[int]]) → list[int]
Examples
in[3,[[0,1],[1,2],[2,0]]]out[0,1,2]
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 40 min
InputExpectedGot
[3,[[0,1],[1,2],[2,0]]][0,1,2]not run yetsample