Coding questions · Capital One
Return the Top Two Teams by Points, Goal Difference, and Tie Breakers
Rank competition teams by points, goal difference, goals scored, and an explicit index tie rule, then return the first two team indices.
Return the indices of the first-place and second-place teams from four arrays of competition statistics.
Team i has wins[i] wins, draws[i] draws, scored[i] goals scored, and conceded[i] goals conceded. Its points are 3 * wins[i] + draws[i], and its goal difference is scored[i] - conceded[i].
Function Signature
rank_top_two(wins: list[int], draws: list[int], scored: list[int], conceded: list[int]) -> list[int]
Ranking Rules
Compare teams using these criteria, in order, with the larger value ranked first:
- Points.
- Goal difference.
- Goals scored.
- Team index.
The goals-scored and larger-index tie rules are explicit conventions for this exercise, matching descending comparison of (points, goal_difference, goals_scored, index).
Output
Return [first_place_index, second_place_index]. Team indices are zero-based, and the two returned indices must be distinct.
Constraints
- All four arrays have the same length
n, with2 <= n <= 100000. - Every array element is an integer in
[0, 1000000]. - Ranking uses the supplied statistics; no additional consistency checks on the competition are required.
Examples
Input: wins = [2,1,2], draws = [0,3,0], scored = [5,8,7], conceded = [2,5,3]
Output: [2,1]
All teams have 6 points. Team 2 has goal difference 4. Teams 0 and 1 have goal difference 3, so team 1 ranks next because it scored more goals.
Input: wins = [0,0,0], draws = [0,0,0], scored = [0,0,0], conceded = [0,0,0]
Output: [2,1]
Top Two Teams by Points, Goal Difference and Goals Scored
You are given four integer arrays of the same length n describing the teams of a competition. Team i (zero-based) has wins[i] wins, draws[i] draws, scored[i] goals scored and conceded[i] goals conceded.
Team i's points are 3 * wins[i] + draws[i], and its goal difference is scored[i] - conceded[i].
Compare teams using these criteria, in order, with the larger value ranked first:
- Points.
- Goal difference.
- Goals scored.
- Team index.
The goals-scored and larger-index tie rules are explicit conventions for this exercise: the ranking is exactly the descending comparison of the tuple (points, goal_difference, goals_scored, index). Because team indices are distinct, no two teams ever compare equal, so the ranking is a strict total order and the answer is unique.
Return [first_place_index, second_place_index], the indices of the first-place and second-place teams in that order. Team indices are zero-based and the two returned indices are always distinct.
All intermediate values fit in a signed 32-bit integer: points are at most 3 * 1000000 + 1000000 = 4000000 and goal difference lies in [-1000000, 1000000]. No value can exceed 2^31 - 1, so Java int and C++ int are sufficient (no long / long long is needed).
Example 1:
Input: wins = [2, 1, 2], draws = [0, 3, 0], scored = [5, 8, 7], conceded = [2, 5, 3]
Output: [2, 1]
Explanation: all teams have 6 points. Team 2 has goal difference 4, while teams 0 and 1 have goal difference 3, so team 2 is first. Between teams 0 and 1 the goal differences tie, so goals scored decides and team 1 (8 goals vs 5) is second.
Example 2:
Input: wins = [0, 0, 0], draws = [0, 0, 0], scored = [0, 0, 0], conceded = [0, 0, 0]
Output: [2, 1]
Explanation: every team ties on points, goal difference and goals scored, so the larger index ranks first: team 2 is first and team 1 is second.
Hints
- Every comparison between two teams is decided by the same fixed sequence of four quantities; computing those quantities once per team makes each comparison uniform.
- The last criterion is the team index, which differs for every team, so no two teams can ever compare equal and the answer is never ambiguous.
- The question asks only for the first two places, not for the position of every team.
Approach
Reduce every team to one comparable record: key(i) = (points, goal_difference, goals_scored, index) with points = 3 * wins[i] + draws[i] and goal_difference = scored[i] - conceded[i]. Ranking the teams is exactly sorting these keys in descending lexicographic order, which reproduces the four stated criteria in order: a tie on points falls through to goal difference, then to goals scored, then to the index. Because the index component is distinct for every team, no two keys are equal, so the order is a strict total order and the first two places are unique.
Algorithm: a single linear scan maintaining the largest key seen so far (best) and the second largest (second). For each team build its key; if it beats best, demote the old best to second and install the new key as best; otherwise, if it beats second, replace second. Invariant: after processing the prefix of teams 0..i, best and second hold the two largest keys among those teams, in order. The invariant is preserved because the only candidates for the new top two are the previous top two plus the new key, and the two-branch update considers exactly those. After the last team the invariant yields the global top two, and the index component of each is returned.
Correctness of the tie rule: teams are visited in increasing index order, so when a new key ties the incumbent on points, goal difference and goals scored, the new (larger) index wins the comparison, matching 'larger index ranks first'. The Python reference relies on native tuple comparison; the JavaScript, Java and C++ references expand the same lexicographic comparison field by field so all four are semantically identical.
Edge cases: n = 2 simply returns the two teams in comparison order; all-identical statistics return [n-1, n-2]; goal difference may be negative (as low as -1000000) and must be compared as a signed value, never as an absolute value; the maximum-points team wins even with the worst goal difference. Points reach at most 4000000, so a signed 32-bit integer is sufficient everywhere and no 64-bit type is required. If fewer than two teams were supplied the references return an empty list, although the stated constraints exclude that input.
- Time complexity: O(n)
- Space complexity: O(1) extra space, excluding the two-element output list