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
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
โก Linearithmic
Space Complexity
O(n)
Space for result set and window function processing
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code