Return the Top Two Teams by Points, Goal Difference, and Tie Breakers
Capital OneSoftware Engineer · Online AssessmentLast updated
Implement a ranking algorithm to evaluate sports competition matches, compute points, goal differential, and total goals for each team, and return the indices of the top two teams using multi-level tie-breakers.
Problem Statement
You are given the results of a round-robin tournament with n teams numbered from 0 to n - 1. You are given an integer n and a 2D integer array matches, where each element matches[i] = [teamA, teamB, scoreA, scoreB] denotes a match played between teamA and teamB, with teamA scoring scoreA goals and teamB scoring scoreB goals.
Points are awarded as follows:
- Win:
3points to the winning team,0points to the losing team. - Draw:
1point to each team ifscoreA == scoreB.
Teams should be ranked according to the following tie-breaking rules, in order of priority:
- Total points earned (higher is better)
- Goal difference (
total goals scored - total goals conceded, higher is better) - Total goals scored (higher is better)
- Team index (smaller index is better)
Return an array containing the indices of the top two teams: [first_place_index, second_place_index].
Function Signature
def get_top_two_teams(n: int, matches: list[list[int]]) -> list[int]:
pass
Input Format
n: An integer representing the number of teams.matches: A list of lists of four integers[teamA, teamB, scoreA, scoreB].
Output Format
- Return a list of two integers
[top1, top2].
Constraints
2 <= n <= 10^41 <= len(matches) <= 10^50 <= teamA, teamB < nandteamA != teamB0 <= scoreA, scoreB <= 100
Examples
Example 1
Input: n = 3, matches = [
[0, 1, 2, 1],
[1, 2, 3, 0],
[0, 2, 1, 1]
]
Output: [0, 1]
Explanation:
- Team 0: beats Team 1 (3 pts, +1 GD, 2 GF), ties Team 2 (1 pt, 0 GD, 1 GF) -> Total: 4 pts, GD = +1, GF = 3.
- Team 1: loses to Team 0 (0 pts, -1 GD, 1 GF), beats Team 2 (3 pts, +3 GD, 3 GF) -> Total: 3 pts, GD = +2, GF = 4.
- Team 2: loses to Team 1 (0 pts, -3 GD, 0 GF), ties Team 0 (1 pt, 0 GD, 1 GF) -> Total: 1 pt, GD = -3, GF = 1.
Ranked: Team 0 (4 pts), Team 1 (3 pts). Top two are
[0, 1].
Example 2
Input: n = 3, matches = [
[0, 1, 2, 2],
[1, 2, 1, 1],
[0, 2, 0, 0]
]
Output: [0, 1]
Explanation:
- Team 0: 2 draws -> 2 pts, GD = 0, GF = 2.
- Team 1: 2 draws -> 2 pts, GD = 0, GF = 3.
- Team 2: 2 draws -> 2 pts, GD = 0, GF = 1.
All have 2 points and 0 GD. Team 1 has 3 GF (1st place). Team 0 has 2 GF (2nd place). Thus,
[1, 0]is returned.
Edge Cases
- Teams that play no matches retain
0points,0GD, and0GF. - Complete ties between teams are resolved by smaller team index.
Hints
Hint 1
First, aggregate the statistics for each team by iterating through the matches array and updating points, goals scored, and goals conceded.
Hint 2
Formulate a tuple or key for each team that allows sorting in a single pass: `(-points, -goal_difference, -goals_scored, team_index)`.
Hint 3
Since you only need the top two teams, you can either sort all teams or find the two best elements in a single `O(n)` linear scan.
Follow-up questions
- How can you optimize the solution to run in O(n + m) time instead of O(n log n) when n is large?
- How would you handle head-to-head match outcomes as an additional tiebreaker between teams tied on points and goal difference?
- If match results were streamed in real time and queries for the top two teams were frequent, what data structure would you use to maintain the standings efficiently?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.