Block OA Interview Problem: Tournament Ranking Comparison

17 Views
No Comments

Say we have a tournament of some sort. It doesn’t really matter what the tournament is for; all we need to know is that each game in the tournament is 1:1.

At the end of a game, each player has a non-negative score (0 or higher). We’ll use the usual rules of higher score wins, and it’s perfectly possible that a game results in a tie.

To keep things fair, at the end of the tournament, everybody has played the same number of games.

Given the results of a tournament and the IDs of two players, we’d like to know which of those two players did better in the overall tournament.

Game results are in the format {player1, score1, player2, score2}.

If Alice and Bob finish the tournament with the same record, but Alice played Bob and won, then we say that Alice did better than Bob overall.

The key idea is to compare two players by their overall tournament record. Since each match is one-on-one and may end in a win, loss, or tie, we can aggregate wins and losses for every player from the match list. If the two target players have the same record, a direct head-to-head result breaks the tie: the winner of their matchup is considered to have performed better overall. A hash map is a natural fit for storing each player's stats while scanning the tournament results once.

END
 0