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
Football Pass Streak AnalysisA1A2A3A4C1C2C3C4123XStreak AnalysisArsenal: 3 passes(A1โ†’A2โ†’A3โ†’A4)Chelsea: 4 passes(C1โ†’C2โ†’C3โ†’C4)โœ“ Same teamโœ— InterceptedTeams๐Ÿ”ด Arsenal (A1,A2,A3,A4)๐Ÿ”ต Chelsea (C1,C2,C3,C4)
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.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
28.4K 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