Longest Winning Streak - Problem
Longest Winning Streak - Find the maximum consecutive wins for each player
You are given a table
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:
The
Return: A table with
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
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
โก Linearithmic
Space Complexity
O(n)
Storage for window function calculations and final results
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code