← Questions

Knockout tournament by rank, then build the bracket

0
UberCodingLast asked May 2026

A coding interview question asked in Uber's technical phone screen, last asked May 2026. It tests simulation and recursion.

Players enter a single-elimination tournament in a given starting order. Each player has a rank, and a smaller rank is a better player who always wins. In every round, neighbours in the current order play each other (positions 0 and 1, 2 and 3, and so on) and the winner advances, keeping that order, until one player is left. Players keep their original identifiers throughout, and the last part moves from code to storing the match records in a database.

Part 1: simulate the tournament

Unless you agree on a different interface with the interviewer, assume:

  • simulate(ranks: list[int]) -> list[list[int]] — ranks[i] is the rank of player i, and players start in index order. Returns, for each round, the indices of the players who advanced, in order. With an odd number of players, the last one gets a bye into the next round.
simulate([4, 1, 3, 5, 2])  ->  [[1, 2, 4], [1, 4], [1]]
# round 1: player 0 (rank 4) loses to player 1 (rank 1), player 3 loses to player 2, player 4 has a bye
# round 2: player 1 beats player 2, player 4 has a bye
# round 3: player 1 (rank 1) beats player 4 (rank 2)

Part 2: build a seeded bracket

  • seed(n: int) -> list[int] — n is a power of two. Return a starting order of the ranks 1..n such that the best players meet as late as possible: after round j, exactly the n / 2^j best ranks are left. Any valid order is accepted.
seed(8)  ->  [1, 8, 4, 5, 2, 7, 3, 6]
# round 1 leaves ranks 1, 4, 2, 3; round 2 leaves 1, 2; rank 1 wins the final

Part 3: store the matches

Design tables that record every match (tournament, round, the two players, the winner) and write SQL for: all matches a player took part in, and the round in which each player was knocked out.

Constraints

  • 1 <= len(ranks) <= 10^5; ranks are distinct.

Clarifying questions

  • Does the bye go to the last player or the best-ranked one?
  • Should the output hold indices, ranks or player names?
  • Does the winner keep the position of the left or the right player? (It does not change the result here.)
  • Can two players share a rank?

What a strong answer covers

  • Part 1: a loop over rounds that builds the next list from pairs, O(n) total since each round halves the field.
  • Keeping player identity separate from rank, so output never confuses the two.
  • Part 2: the recursive construction: from an order for n/2, replace each rank r by the pair r, n + 1 − r. Proving it with Part 1's simulator.
  • Part 3: a players table, a matches table with foreign keys and an index on (tournament_id, player), and queries using joins or GROUP BY.
  • Tests: one player, two players, odd counts at several rounds.

Follow-ups

  • n is not a power of two. Give byes to the best players in round one.
  • Results are random: the better rank wins with a probability based on the rank gap. Estimate each player's chance of winning.
  • Print the bracket as text, round by round.

Where it's asked

  • Uber · Technical phone screen · Last asked May 202660 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design

What it tests

simulation · recursion

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding