Longest Team Pass Streak - Problem
You're analyzing pass sequences in a football match to determine which team maintained the longest consecutive successful passes! ๐
Given two database tables:
- Teams: Contains player IDs and their team affiliations
- Passes: Contains chronological pass records with timestamps
A successful pass streak occurs when consecutive passes happen between players of the same team. The streak breaks when:
- A pass is intercepted (received by an opposing team player)
Goal: Find the longest successful pass streak for each team during the match.
Input: Two SQL tables with player-team mappings and chronological pass data
Output: Table showing each team's longest consecutive pass streak, ordered by team name
Example: If Arsenal passes 1โ2โ3โ4โ(intercepted by Chelsea), then 1โ2โ3, their longest streak is 3 passes.
Input & Output
example_1.sql โ Basic Case
$
Input:
Teams: [(1,'Arsenal'), (2,'Arsenal'), (3,'Arsenal'), (4,'Arsenal'), (5,'Chelsea'), (6,'Chelsea')]
Passes: [(1,'00:05',2), (2,'00:07',3), (3,'00:08',4), (4,'00:10',5)]
โบ
Output:
Arsenal: 3, Chelsea: 0
๐ก Note:
Arsenal has 3 consecutive passes (1โ2โ3โ4) before 4 passes to Chelsea's player 5, breaking the streak
example_2.sql โ Multiple Streaks
$
Input:
Teams: [(1,'Arsenal'), (2,'Arsenal'), (5,'Chelsea'), (6,'Chelsea')]
Passes: [(1,'00:05',2), (2,'00:07',5), (5,'00:10',6), (6,'00:12',1), (1,'00:15',2)]
โบ
Output:
Arsenal: 2, Chelsea: 1
๐ก Note:
Arsenal: 1โ2 (1 pass), then 1โ2 (1 pass) = max 1. Chelsea: 5โ6 (1 pass). Wait, Arsenal gets 2 from the final sequence.
example_3.sql โ Single Team
$
Input:
Teams: [(1,'Arsenal'), (2,'Arsenal'), (3,'Arsenal')]
Passes: [(1,'00:05',2), (2,'00:07',3), (3,'00:10',1)]
โบ
Output:
Arsenal: 3
๐ก Note:
All passes are within Arsenal, creating one streak of 3 consecutive passes
Constraints
- 1 โค number of players โค 100
- 1 โค number of passes โค 1000
- All timestamps are in format MM:SS between 00:00 and 90:00
- Each pass has a unique (pass_from, time_stamp) combination
- All player_ids in Passes table exist in Teams table
Visualization
Tap to expand
Understanding the Visualization
1
Map players to teams
First, we need to know which team each player belongs to
2
Track pass sequences
Follow the chronological order of passes during the match
3
Identify streaks
Count consecutive passes that stay within the same team
4
Find longest streaks
For each team, determine their longest successful passing sequence
Key Takeaway
๐ฏ Key Insight: Use window functions to detect possession changes efficiently - LAG() identifies when the ball switches teams, and running sums group consecutive same-team passes for easy counting.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code