Simulate and Construct Tournament Rankings
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of tournament simulation and constructive permutation design, testing skills in array manipulation, sequential pairwise elimination reasoning, and combinatorial ordering of eliminations.
Part 1: Simulate Tournament Rounds
Constraints
- 1 <= len(ranks) <= 2 * 10^5
- ranks is a permutation of 1..len(ranks)
- The total number of values across all returned rounds is O(n)
Examples
Input: [1, 2, 3, 4, 5, 6, 7, 8]
Expected Output: [[1, 3, 5, 7], [1, 5], [1]]
Explanation: Each adjacent pair keeps the smaller rank. Repeating that process produces the three rounds shown.
Input: [5, 1, 4, 2, 3]
Expected Output: [[1, 2, 3], [1, 3], [1]]
Explanation: Round 1 winners are 1 and 2, and rank 3 gets a bye. In round 2, rank 1 beats rank 2 and rank 3 gets another bye.
Input: [3, 2]
Expected Output: [[2]]
Explanation: There is only one match, and the smaller rank wins.
Input: [1]
Expected Output: []
Explanation: With only one player, no rounds are played.
Hints
- Build the next round by scanning the current round two players at a time.
- If there is no partner for the last player in a round, copy that player directly into the next round.
Part 2: Construct Tournament Rankings with Monotonic Survival
Constraints
- 1 <= n <= 2 * 10^5
- The returned list must be a permutation of 1..n
- For any ranks a < b, player a must survive at least as many rounds as player b
Examples
Input: 1
Expected Output: [1]
Explanation: Only one player exists, so the permutation is forced.
Input: 2
Expected Output: [1, 2]
Explanation: Rank 1 defeats rank 2 in the only match.
Input: 5
Expected Output: [1, 5, 3, 4, 2]
Explanation: Round 1 survivors are [1, 3, 2], then [1, 2], then [1]. Stronger ranks never leave earlier than weaker ones.
Input: 7
Expected Output: [1, 7, 4, 6, 2, 5, 3]
Explanation: Round 1 survivors are [1, 4, 2, 3], then [1, 2], then [1].
Input: 8
Expected Output: [1, 8, 4, 7, 2, 6, 3, 5]
Explanation: Ranks 5 through 8 fall in round 1, ranks 3 and 4 fall in round 2, rank 2 falls in round 3, and rank 1 is champion.
Hints
- After the first round, exactly ceil(n / 2) players remain. If stronger players can never leave earlier than weaker ones, which ranks should those survivors be?
- Once you choose the first-round survivors, the same construction problem appears again on a smaller tournament.