The Change in Global Rankings - Problem

Imagine you're working for an international sports federation tasked with tracking global team rankings! ๐Ÿ†

You have two tables:

  • TeamPoints: Contains each team's current points and country name
  • PointsChange: Contains point changes for each team (positive = increase, negative = decrease, 0 = no change)

Teams are ranked by their points in descending order. If two teams have the same points, they're ordered alphabetically by country name.

Your mission: Calculate how much each team's global ranking position changed after applying the point updates!

For example, if Team A was ranked #3 before the update and #1 after, their ranking change is 3 - 1 = 2 (positive means they moved up).

Return a table showing each team's name and their ranking change, where:

  • Positive values = moved up in rankings
  • Negative values = moved down in rankings
  • Zero = same ranking position

Input & Output

example_1.sql โ€” Basic Ranking Changes
$ Input: TeamPoints: | team_id | name | points | |---------|-----------|--------| | 1 | Brazil | 10 | | 2 | Argentina | 8 | | 3 | France | 12 | PointsChange: | team_id | points_change | |---------|---------------| | 1 | 2 | | 2 | -1 | | 3 | 0 |
โ€บ Output: | name | ranking_change | |-----------|----------------| | Argentina | -1 | | Brazil | 1 | | France | 0 |
๐Ÿ’ก Note: Initially: France(12) #1, Brazil(10) #2, Argentina(8) #3. After changes: Brazil(12) #1, France(12) #2, Argentina(7) #3. France stays #2 due to alphabetical tiebreaker with Brazil. Brazil moves up 1 position (2-1=1), Argentina moves down 0 positions (3-3=0), France stays same (1-2=-1... wait, let me recalculate: France goes from #1 to #2, so 1-2=-1).
example_2.sql โ€” Tie Breaking Scenario
$ Input: TeamPoints: | team_id | name | points | |---------|---------|--------| | 1 | Croatia | 15 | | 2 | Belgium | 15 | | 3 | Denmark | 12 | PointsChange: | team_id | points_change | |---------|---------------| | 1 | -3 | | 2 | 0 | | 3 | 3 |
โ€บ Output: | name | ranking_change | |---------|----------------| | Belgium | 0 | | Croatia | -1 | | Denmark | 2 |
๐Ÿ’ก Note: Initially (tied at 15, alphabetical): Belgium #1, Croatia #2, Denmark #3. After changes: Denmark(15) #1, Belgium(15) #2, Croatia(12) #3. Belgium moves from #1 to #2 (1-2=-1), Croatia from #2 to #3 (2-3=-1), Denmark from #3 to #1 (3-1=2).
example_3.sql โ€” All Same Points Edge Case
$ Input: TeamPoints: | team_id | name | points | |---------|-----------|--------| | 1 | Australia | 10 | | 2 | Canada | 10 | | 3 | Japan | 10 | PointsChange: | team_id | points_change | |---------|---------------| | 1 | 0 | | 2 | 0 | | 3 | 0 |
โ€บ Output: | name | ranking_change | |-----------|----------------| | Australia | 0 | | Canada | 0 | | Japan | 0 |
๐Ÿ’ก Note: All teams have same points before and after, so rankings remain: Australia #1, Canada #2, Japan #3 (alphabetical order). No ranking changes occur.

Visualization

Tap to expand
Initial Rankings๐Ÿฅ‡ France (12)๐Ÿฅˆ Brazil (10)๐Ÿฅ‰ Argentina (8)Point ChangesFrance: +0Brazil: +2Argentina: -1New Rankings๐Ÿฅ‡ Brazil (12)๐Ÿฅˆ France (12)๐Ÿฅ‰ Argentina (7)Ranking ChangesBrazil: 2 โ†’ 1 = +1 (moved up)France: 1 โ†’ 2 = -1 (moved down)Argentina: 3 โ†’ 3 = 0 (same)
Understanding the Visualization
1
Initial Rankings
Teams are ranked by points (highest first), with ties broken alphabetically
2
Apply Changes
Each team receives positive, negative, or zero point changes
3
New Rankings
Recalculate rankings with updated points using same rules
4
Calculate Shifts
Find difference: old_rank - new_rank (positive = moved up)
Key Takeaway
๐ŸŽฏ Key Insight: Use window functions to calculate both rankings simultaneously, then subtract to find position changes efficiently in a single query.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single sort operation for window function ranking

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for result set and window function processing

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค number of teams โ‰ค 104
  • Team names contain only letters and are unique
  • -103 โ‰ค points_change โ‰ค 103
  • Each team_id in TeamPoints appears exactly once in PointsChange
  • 1 โ‰ค original points โ‰ค 106
Asked in
Google 25 Meta 18 Amazon 22 Microsoft 15
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen