The Game Play Analysis III interview question asks you to calculate the cumulative sum (running total) of games played by each player for every date they logged in. The output should include the player_id, the event_date, and the total number of games played by that player up to and including that date.
This is a classic Database coding problem to test a candidate's knowledge of Window Functions, specifically for calculating running totals. While it can be solved with self-joins, that approach is outdated and inefficient. Interviewers want to see if you are familiar with modern SQL analytics functions that handle cumulative data seamlessly.
The standard pattern here is the Cumulative Sum Window Function.
SUM() aggregate function, but apply it as a window function using the OVER() clause.player_id so the sum resets for each new player.event_date to ensure the sum accumulates chronologically.
SUM(games_played) OVER (PARTITION BY player_id ORDER BY event_date) AS games_played_so_farPlayer 1 Logs:
Window Function Execution:
JOIN where a.player_id = b.player_id AND a.event_date >= b.event_date and then grouping. This works but has complexity per player, which is terrible for large datasets.OVER(PARTITION BY player_id), it will just return the grand total for the player on every row, not a running total. The ORDER BY is required to frame the window chronologically.Memorize the syntax for running totals: SUM(col) OVER (PARTITION BY group_col ORDER BY time_col). It is one of the top 3 most useful window functions to know for data interviews.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Game Play Analysis IV | Medium | Solve | |
| Active Businesses | Medium | Solve | |
| Active Users | Medium | Solve | |
| Activity Participants | Medium | Solve | |
| All People Report to the Given Manager | Medium | Solve |