Longest Winning Streak - Problem

You have a table Matches that contains information about players and their match results:

  • player_id: The ID of the player
  • match_day: The date when the match was played
  • result: The result of the match ('Win', 'Draw', 'Lose')

The winning streak of a player is the number of consecutive wins uninterrupted by draws or losses.

Write an SQL query to count the longest winning streak for each player.

Return the result table in any order.

Table Schema

Matches
Column Name Type Description
player_id PK int ID of the player
match_day PK date Date when the match was played
result enum Result of the match: 'Win', 'Draw', or 'Lose'
Primary Key: (player_id, match_day)
Note: Each row represents one match result for a player on a specific day

Input & Output

Example 1 — Mixed Results with Multiple Streaks
Input Table:
player_id match_day result
1 2022-01-17 Win
1 2022-01-18 Win
1 2022-01-25 Win
1 2022-01-31 Draw
1 2022-02-01 Win
2 2022-02-01 Win
2 2022-02-02 Lose
2 2022-02-03 Win
Output:
player_id longest_winning_streak
1 3
2 1
💡 Note:

Player 1 has wins on Jan 17-18 and Jan 25 (3 consecutive wins), then a draw breaks the streak. Player 2's longest streak is 1 win since the loss on Feb 2 breaks any potential streak.

Example 2 — No Wins
Input Table:
player_id match_day result
1 2022-01-17 Draw
1 2022-01-18 Lose
2 2022-02-01 Lose
Output:
player_id longest_winning_streak
1 0
2 0
💡 Note:

Both players have no wins, so their longest winning streaks are 0.

Constraints

  • 1 ≤ player_id ≤ 10000
  • match_day is a valid date
  • result is one of 'Win', 'Draw', or 'Lose'
  • 1 ≤ number of matches ≤ 1000

Visualization

Tap to expand
Longest Winning Streak INPUT player_id day result 1 1 Win 1 2 Win 1 3 Win 1 4 Lose 1 5 Win 2 1 Win 2 2 Draw 2 3 Win 2 4 Win Player 1 Timeline: W W W L W Streak: 3 ALGORITHM STEPS 1 Order by player, day Sort matches chronologically 2 Assign streak groups Reset group on non-Win 3 Count consecutive Wins Within each streak group 4 Find MAX per player Group by player_id Window Function Approach: SUM(CASE WHEN result != 'Win' THEN 1 ELSE 0 END) OVER ( PARTITION BY player_id ORDER BY match_day ) AS grp Creates unique groups for streaks FINAL RESULT Output Table: player_id streak 1 3 2 2 Verification: Player 1: W-W-W-L-W Max streak = 3 OK Player 2: W-D-W-W Max streak = 2 OK Time: O(n log n) Key Insight: Use a running count of non-Win results as a "group identifier" for each streak. When a player loses or draws, the group number increments, creating boundaries between winning streaks. Then simply count consecutive Wins within each group and take the maximum per player using window functions. TutorialsPoint - Longest Winning Streak | Optimal Solution
Asked in
Facebook 28 Amazon 22 Google 18
34.5K Views
Medium Frequency
~25 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