Game Play Analysis III - Problem
Game Play Analysis III - Calculate Running Totals of Games Played

You're analyzing player activity data for a mobile gaming platform. Given a table of player login records, you need to calculate running totals of games played by each player over time.

Table: Activity
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| player_id | int |
| device_id | int |
| event_date | date |
| games_played | int |
+--------------+---------+


The primary key is (player_id, event_date). Each row represents a player's gaming session on a specific date.

Goal: For each player and date, calculate the cumulative total of games played from their first login up to that date. This gives us insights into player engagement patterns over time.

Example: If Player 1 played 5 games on Jan 1st and 3 games on Jan 3rd, the result should show 5 games for Jan 1st and 8 games (5+3) for Jan 3rd.

Input & Output

example_1.sql โ€” Basic Player Activity
$ Input: Activity table: +-----------+-----------+------------+--------------+ | player_id | device_id | event_date | games_played | +-----------+-----------+------------+--------------+ | 1 | 2 | 2016-03-01 | 5 | | 1 | 2 | 2016-05-02 | 6 | | 2 | 3 | 2017-06-25 | 1 | | 3 | 1 | 2016-03-02 | 0 | | 3 | 4 | 2018-07-03 | 5 | +-----------+-----------+------------+--------------+
โ€บ Output: +-----------+------------+---------------------+ | player_id | event_date | games_played_so_far | +-----------+------------+---------------------+ | 1 | 2016-03-01 | 5 | | 1 | 2016-05-02 | 11 | | 2 | 2017-06-25 | 1 | | 3 | 2016-03-02 | 0 | | 3 | 2018-07-03 | 5 | +-----------+------------+---------------------+
๐Ÿ’ก Note: Player 1: First login with 5 games (total: 5), second login with 6 more games (total: 5+6=11). Player 2: Only one session with 1 game. Player 3: First session with 0 games, second session adds 5 games for a total of 5.
example_2.sql โ€” Single Player Multiple Days
$ Input: Activity table: +-----------+-----------+------------+--------------+ | player_id | device_id | event_date | games_played | +-----------+-----------+------------+--------------+ | 1 | 1 | 2020-01-01 | 3 | | 1 | 1 | 2020-01-02 | 2 | | 1 | 1 | 2020-01-03 | 1 | +-----------+-----------+------------+--------------+
โ€บ Output: +-----------+------------+---------------------+ | player_id | event_date | games_played_so_far | +-----------+------------+---------------------+ | 1 | 2020-01-01 | 3 | | 1 | 2020-01-02 | 5 | | 1 | 2020-01-03 | 6 | +-----------+------------+---------------------+
๐Ÿ’ก Note: Player 1 plays consistently over 3 days. Running totals: Day 1: 3 games, Day 2: 3+2=5 games, Day 3: 3+2+1=6 games total.
example_3.sql โ€” Zero Games Edge Case
$ Input: Activity table: +-----------+-----------+------------+--------------+ | player_id | device_id | event_date | games_played | +-----------+-----------+------------+--------------+ | 1 | 1 | 2021-01-01 | 0 | | 1 | 1 | 2021-01-02 | 0 | | 1 | 1 | 2021-01-03 | 5 | +-----------+-----------+------------+--------------+
โ€บ Output: +-----------+------------+---------------------+ | player_id | event_date | games_played_so_far | +-----------+------------+---------------------+ | 1 | 2021-01-01 | 0 | | 1 | 2021-01-02 | 0 | | 1 | 2021-01-03 | 5 | +-----------+------------+---------------------+
๐Ÿ’ก Note: Player 1 logs in but doesn't play games for first two days (running total stays 0), then plays 5 games on third day (total becomes 5).

Visualization

Tap to expand
๐Ÿฆ Bank Statement Analogy - Running TotalsPlayer 1 Gaming StatementDate | Games | Running Total2016-03-01 | +5 | 5 games2016-05-02 | +6 | 11 gamesโœ“Window Function ProcessSUM(games_played) OVER (PARTITION BY player_id ORDER BY date)Player 2 Gaming StatementDate | Games | Running Total2017-06-25 | +1 | 1 gameโœ“Key InsightEach player's statement iscalculated independentlyusing partitioning๐Ÿ’ก Algorithm Visualization1Partition data by player (like separating bank accounts)2Sort each partition by date (chronological order)3Apply running sum within each partition4Output: Each row shows cumulative total up to that dateWindow Function Magic
Understanding the Visualization
1
Group by Account (Player)
Separate transactions by account holder, like grouping bank statements by customer
2
Sort by Date
Arrange transactions chronologically to see the sequence of events
3
Calculate Running Balance
Add each transaction to the previous balance to get the new total
4
Display Statement
Show date, transaction amount, and running balance for each entry
Key Takeaway
๐ŸŽฏ Key Insight: Window functions eliminate the need for self-joins by efficiently calculating running sums in a single pass, making this optimal for large datasets just like modern banking systems process millions of transactions efficiently.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single scan with sorting by player and date. Database optimizes the window function execution

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for sorting and intermediate window function calculations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค Activity table rows โ‰ค 1000
  • 1 โ‰ค player_id โ‰ค 1000
  • 1 โ‰ค device_id โ‰ค 1000
  • 0 โ‰ค games_played โ‰ค 1000
  • event_date is in format 'YYYY-MM-DD'
  • (player_id, event_date) is unique
Asked in
Amazon 25 Google 18 Meta 12 Microsoft 8
28.4K Views
Medium Frequency
~15 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