The Change in Global Rankings - Problem

You are given two tables: TeamPoints containing national teams and their current points, and PointsChange containing point changes for each team.

The global ranking is determined by:

  • Teams sorted by points in descending order
  • Ties broken by team name in lexicographical order

Your task is to:

  1. Update each team's points using the points_change values
  2. Calculate the ranking before and after the update
  3. Return the change in ranking for each team

Return the result showing team_id, name, and ranking_change (negative means rank improved, positive means rank worsened).

Table Schema

TeamPoints
Column Name Type Description
team_id PK int Unique team identifier
name varchar Country name the team represents
points int Current points in global rankings
Primary Key: team_id
PointsChange
Column Name Type Description
team_id PK int Unique team identifier
points_change int Change in points (positive=increase, negative=decrease, 0=no change)
Primary Key: team_id

Input & Output

Example 1 — Basic Ranking Changes
Input Tables:
TeamPoints
team_id name points
1 Brazil 80
2 Germany 60
3 Spain 40
PointsChange
team_id points_change
1 -20
2 25
3 -10
Output:
team_id name ranking_change
1 Brazil 1
2 Germany -1
3 Spain 0
💡 Note:

Initially: Brazil (80pts, rank 1), Germany (60pts, rank 2), Spain (40pts, rank 3). After changes: Germany (85pts, rank 1), Brazil (60pts, rank 2), Spain (30pts, rank 3). Germany improved by 1 rank (-1), Brazil dropped by 1 rank (+1), Spain stayed the same (0).

Example 2 — Tie Breaking by Name
Input Tables:
TeamPoints
team_id name points
1 Italy 50
2 France 50
PointsChange
team_id points_change
1 0
2 0
Output:
team_id name ranking_change
1 Italy 0
2 France 0
💡 Note:

Both teams have 50 points before and after changes. Since points are equal, rankings are determined by name lexicographically: France (rank 1), Italy (rank 2). No point changes means no ranking changes.

Constraints

  • 1 ≤ team_id ≤ 1000
  • 1 ≤ name.length ≤ 100
  • 0 ≤ points ≤ 1000
  • -1000 ≤ points_change ≤ 1000
  • Each team in TeamPoints appears exactly once in PointsChange

Visualization

Tap to expand
The Change in Global Rankings INPUT id name pts new 1 TeamA 100 120 2 TeamB 150 140 3 TeamC 130 160 4 TeamD 120 115 Old Rankings (by pts desc) 1:B 2:C 3:D 4:A Ranking Rules: 1. Sort by points (DESC) 2. Ties: sort by name (ASC) ALGORITHM STEPS 1 Calculate Old Ranks Sort teams by old points B:1, C:2, D:3, A:4 2 Update Points Apply new point values A:120, B:140, C:160, D:115 3 Calculate New Ranks Re-sort by new points C:1, B:2, A:3, D:4 4 Compute Rank Change old_rank - new_rank negative = improved Rank Comparison A: 4-->3 = +1 (worse) B: 1-->2 = -1 (better) C: 2-->1 = +1 (worse) D: 3-->4 = -1 (better) FINAL RESULT New Rankings (by new pts) 1:C 2:B 3:A 4:D Output Table id name change 1 TeamA +1 2 TeamB -1 3 TeamC +1 4 TeamD -1 Legend Negative: Rank Up Positive: Rank Down OK - Rankings Updated Key Insight: Rank change = old_rank - new_rank. A negative value means the team moved UP in rankings (improved position). Sorting twice with same criteria (pts DESC, name ASC) ensures consistent tie-breaking. Time complexity: O(n log n) for sorting operations. TutorialsPoint - The Change in Global Rankings | Optimal Solution
Asked in
Amazon 23 Google 18 Microsoft 15
28.5K Views
Medium Frequency
~18 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