Longest Winning Streak - Problem
Longest Winning Streak - Find the maximum consecutive wins for each player

You are given a table Matches that tracks player match results over time. Each row contains a player's ID, the date they played, and whether they won, lost, or drew that match.

Goal: Calculate the longest winning streak for each player. A winning streak is the number of consecutive wins uninterrupted by any draws or losses.

Table Schema:
+-------------+------+
| Column Name | Type |
+-------------+------+
| player_id | int |
| match_day | date |
| result | enum |
+-------------+------+


The result column contains values: 'Win', 'Draw', or 'Lose'

Return: A table with player_id and their longest_streak (maximum consecutive wins).

Input & Output

example_1.sql โ€” Basic Example
$ Input: Matches table: +----------+------------+--------+ | player_id| match_day | result | +----------+------------+--------+ | 1 | 2022-01-01 | Win | | 1 | 2022-01-02 | Win | | 1 | 2022-01-03 | Lose | | 1 | 2022-01-04 | Win | | 2 | 2022-01-01 | Win | | 2 | 2022-01-02 | Draw | | 2 | 2022-01-03 | Win | +----------+------------+--------+
โ€บ Output: +----------+---------------+ | player_id| longest_streak| +----------+---------------+ | 1 | 2 | | 2 | 1 | +----------+---------------+
๐Ÿ’ก Note: Player 1 has two consecutive wins (Jan 1-2), then loses, then wins once. Player 2 wins once, draws (breaking streak), then wins once. Maximum streaks are 2 and 1 respectively.
example_2.sql โ€” No Wins
$ Input: Matches table: +----------+------------+--------+ | player_id| match_day | result | +----------+------------+--------+ | 3 | 2022-01-01 | Lose | | 3 | 2022-01-02 | Draw | | 3 | 2022-01-03 | Lose | +----------+------------+--------+
โ€บ Output: +----------+---------------+ | player_id| longest_streak| +----------+---------------+ | 3 | 0 | +----------+---------------+
๐Ÿ’ก Note: Player 3 never wins any matches, so their longest winning streak is 0.
example_3.sql โ€” Perfect Streak
$ Input: Matches table: +----------+------------+--------+ | player_id| match_day | result | +----------+------------+--------+ | 4 | 2022-01-01 | Win | | 4 | 2022-01-02 | Win | | 4 | 2022-01-03 | Win | | 4 | 2022-01-04 | Win | +----------+------------+--------+
โ€บ Output: +----------+---------------+ | player_id| longest_streak| +----------+---------------+ | 4 | 4 | +----------+---------------+
๐Ÿ’ก Note: Player 4 wins all matches consecutively, so their longest winning streak equals their total number of matches (4).

Visualization

Tap to expand
Winning Streak Detection VisualizationPlayer 1 Match Timeline:WJan 1WJan 2LJan 3WJan 4Streak Group Analysis:Streak Group 1: 2 winsBreak: 1 lossStreak Group 2: 1 winWindow Function Process:1. LAG(result) detects when previous result โ‰  'Win' โ†’ streak start2. SUM(CASE WHEN streak_start THEN 1 ELSE 0 END) โ†’ group numbers3. COUNT(*) OVER (PARTITION BY group_id) โ†’ streak lengths4. MAX(streak_length) โ†’ longest streak per playerResult: Player 1 longest streak = MAX(2, 1) = 2Algorithm Complexity:โฑ๏ธ Time: O(n log n)๐Ÿ’พ Space: O(n)๐Ÿ”„ Single pass through dataโšก Optimal for large datasets
Understanding the Visualization
1
Scan Match History
Go through each player's matches in chronological order
2
Detect Streak Breaks
Use LAG() to see if previous match result differs from current
3
Group Consecutive Wins
Assign same group ID to consecutive wins using running sum
4
Count Group Sizes
Count wins in each group to get streak lengths
5
Find Maximum
Get the longest streak for each player
Key Takeaway
๐ŸŽฏ Key Insight: Window functions allow us to detect pattern breaks and group consecutive sequences efficiently. The LAG() function is crucial for identifying when streaks start or end, making this a classic application of SQL analytics functions.

Time & Space Complexity

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

One pass through data with sorting for window functions

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

Storage for window function calculations and final results

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค number of players โ‰ค 104
  • 1 โ‰ค matches per player โ‰ค 1000
  • All match_day values are valid dates
  • result column contains only 'Win', 'Draw', or 'Lose'
  • Each (player_id, match_day) combination is unique
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28 Netflix 22
43.6K Views
High Frequency
~25 min Avg. Time
1.8K 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