Approach
Depth-first search
For Longest Team Pass Streak, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 51 lines of SQL from the credited upstream file 3390.sql.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1WITH RECURSIVE2 3 TeamPasses AS (4 SELECT5 Team1.team_name AS team1,6 Team2.team_name AS team2,7 Passes.time_stamp8 FROM Passes9 INNER JOIN Teams AS Team110 ON (Passes.pass_from = Team1.player_id)11 INNER JOIN Teams AS Team212 ON (Passes.pass_to = Team2.player_id)13 ),14 15 Ranked AS (16 SELECT17 team1,18 team2,19 RANK() OVER(PARTITION BY team1 ORDER BY time_stamp) AS `rank`20 FROM TeamPasses21 ),22 23 PassStreaks AS (24 25 SELECT26 team1,27 team2,28 `rank`,29 IF(team1 = team2, 1, 0) AS streak30 FROM Ranked31 WHERE `rank` = 132 UNION ALL33 34 SELECT35 Ranked.team1,36 Ranked.team2,37 Ranked.`rank`,38 IF(Ranked.team1 = Ranked.team2, PassStreaks.streak + 1, 0) AS streak39 FROM Ranked40 INNER JOIN PassStreaks41 ON (42 Ranked.`rank` = PassStreaks.`rank` + 143 AND Ranked.team1 = PassStreaks.team1)44 )4546SELECT team1 AS team_name, MAX(streak) AS longest_streak47FROM PassStreaks48GROUP BY 149HAVING longest_streak > 050ORDER BY 1;51