Premier League Table Ranking - Problem

You are given a table TeamStats containing information about football teams including their wins, draws, and losses.

Your task: Calculate the points and rank for each team in the Premier League. The scoring system is:

  • Win: 3 points
  • Draw: 1 point
  • Loss: 0 points

Important: Teams with the same points must be assigned the same rank.

Return the result ordered by points in descending order, then by team name in ascending order.

Table Schema

TeamStats
Column Name Type Description
team_id PK int Unique identifier for each team
team_name varchar Name of the football team
matches_played int Total number of matches played
wins int Number of matches won
draws int Number of matches drawn
losses int Number of matches lost
Primary Key: team_id
Note: Each row represents one team's season statistics

Input & Output

Example 1 — Standard League Table
Input Table:
team_id team_name matches_played wins draws losses
1 Arsenal 38 25 8 5
2 Chelsea 38 20 10 8
3 Liverpool 38 22 6 10
Output:
team_name points rank
Arsenal 83 1
Liverpool 72 2
Chelsea 70 3
💡 Note:

Arsenal: 25 wins × 3 + 8 draws × 1 = 83 points (Rank 1)
Liverpool: 22 wins × 3 + 6 draws × 1 = 72 points (Rank 2)
Chelsea: 20 wins × 3 + 10 draws × 1 = 70 points (Rank 3)

Example 2 — Teams with Same Points
Input Table:
team_id team_name matches_played wins draws losses
1 Manchester City 38 28 5 5
2 Arsenal 38 26 6 6
3 Barcelona 38 26 6 6
Output:
team_name points rank
Manchester City 89 1
Arsenal 84 2
Barcelona 84 2
💡 Note:

Manchester City leads with 89 points (Rank 1). Both Arsenal and Barcelona have 84 points, so they both get Rank 2. Note that Arsenal comes first alphabetically in the output.

Constraints

  • 1 ≤ team_id ≤ 1000
  • 1 ≤ team_name.length ≤ 50
  • 0 ≤ wins, draws, losses ≤ 50
  • matches_played = wins + draws + losses

Visualization

Tap to expand
Premier League Table Ranking INPUT Team W D L Pts Arsenal 5 2 1 ? Chelsea 4 3 1 ? Liverpool 5 2 1 ? Man City 6 1 1 ? Tottenham 3 4 1 ? Points Formula: Win=3, Draw=1, Loss=0 Points = 3*W + 1*D + 0*L Match Results Array: [ {team:"Arsenal",w:5,d:2,l:1}, {team:"Chelsea",w:4,d:3,l:1}, {team:"Liverpool",w:5,d:2,l:1}, ... ] ALGORITHM STEPS 1 Calculate Points For each team: pts = 3*W + D Arsenal: 3*5+2 = 17 Chelsea: 3*4+3 = 15 Liverpool: 3*5+2 = 17 Man City: 3*6+1 = 19 Tottenham: 3*3+4 = 13 2 Sort Teams By points DESC, name ASC sort((a,b) => b.pts - a.pts || a.name.localeCompare(b.name)) 3 Assign Ranks Same points = same rank if (team.pts == prev.pts) rank = prev.rank // same rank 4 Build Result Return sorted table with ranks FINAL RESULT Rank Team Pts 1 Man City 19 2 Arsenal 17 2 Liverpool 17 4 Chelsea 15 5 Tottenham 13 Tie-Breaking Rule: Arsenal & Liverpool both Rank 2 Output Array: [ {rank:1, team:"Man City", pts:19}, {rank:2, team:"Arsenal", pts:17}, {rank:2, team:"Liverpool", pts:17}, ... ] Key Insight: Teams with equal points share the same rank. The next rank skips ahead based on position. Example: Two teams at rank 2 means next team gets rank 4, not rank 3. Secondary sort by team name (alphabetical) ensures consistent ordering for tied teams. TutorialsPoint - Premier League Table Ranking | Optimal Solution Time Complexity: O(n log n) | Space Complexity: O(n)
Asked in
ESPN 12 Sky Sports 8 FanDuel 15
23.4K Views
Medium Frequency
~12 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