Player Activity Running Totals with Date Gaps
SQL & DataAuto-graded · 16 test casesLast updated
Tests aggregation, running sums, and date-range densification without recursion—core skills for analytics pipelines.
You are given game session records. Each record contains a session identifier, a player identifier, a game identifier, and the date the session occurred.
Your task is to produce two outputs:
Part A: For each player and each date on which they had at least one session, report:
- The player identifier
- The date
- The number of sessions that player had on that date
- The total number of sessions that player has accumulated from their earliest date through the current date (inclusive)
Part B: Extend Part A to include dates with zero activity. Specifically, for each player, include every date from the provided calendar that falls between (and includes) the player's first and last session dates. On dates with no sessions, the session count is zero, but the cumulative total continues from the previous date.
Input
rows: A list of session records. Each record is a list[session_id, player_id, game_id, session_date]wheresession_dateis a string in"YYYY-MM-DD"format.calendar: A list of date strings in"YYYY-MM-DD"format representing available calendar dates.
Output
Return a dictionary with two keys:
"part_a": A list of lists, each containing[player_id, session_date, daily_count, cumulative_count], sorted by player then date."part_b": A list of lists with the same structure, but including zero-activity dates within each player's active range, sorted by player then date.
Constraints
- 1 ≤ number of sessions ≤ 10⁴
- 1 ≤ player_id ≤ 10³
- All dates are valid ISO format strings
- The calendar may contain duplicates or dates outside any player's range
- A player's first and last session dates are determined only by dates where they have at least one session
- Output is sorted by player_id ascending, then session_date ascending
Examples
Example 1
Input: player_activity_totals([[1, 101, 1, '2025-08-29'], [2, 101, 2, '2025-08-29'], [3, 101, 1, '2025-08-30'], [4, 102, 2, '2025-08-30'], [5, 101, 3, '2025-09-01'], [6, 102, 2, '2025-09-01']], ['2025-08-29', '2025-08-30', '2025-08-31', '2025-09-01'])
Output: {'part_a': [[101, '2025-08-29', 2, 2], [101, '2025-08-30', 1, 3], [101, '2025-09-01', 1, 4], [102, '2025-08-30', 1, 1], [102, '2025-09-01', 1, 2]], 'part_b': [[101, '2025-08-29', 2, 2], [101, '2025-08-30', 1, 3], [101, '2025-08-31', 0, 3], [101, '2025-09-01', 1, 4], [102, '2025-08-30', 1, 1], [102, '2025-08-31', 0, 1], [102, '2025-09-01', 1, 2]]}
Hints
Hint 1
First group sessions by player and date to get daily counts, then compute running totals over those aggregated rows.
Hint 2
For Part B, determine each player's earliest and latest session dates, then iterate through calendar dates that fall in that range.
Hint 3
Use a dictionary lookup with a default of zero to handle dates with no activity when building Part B.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The solution has two distinct phases:
-
Aggregate sessions by player and date. Count how many sessions each player had on each date. This gives us daily counts keyed by
(player_id, date). -
Compute running totals. For each player, sort their dates and accumulate the daily counts to produce cumulative totals.
For Part A, we iterate only over dates where the player had activity.
For Part B, we extend the process: determine each player's first and last active dates, then walk through every calendar date in that range. On dates with no activity, the daily count is zero, but the cumulative total carries forward.
Why It Works
- Aggregation step eliminates duplicate rows. Each
(player, date)pair appears exactly once after grouping, even if the player had multiple sessions that day. - Sorting by date ensures correct accumulation. Since dates are ISO strings (
"YYYY-MM-DD"), lexicographic order matches chronological order. - The running total at date d equals the sum of all daily counts up to and including d. This is the definition of cumulative sessions.
- Part B range logic is exact. We only include calendar dates that fall within
[first_active_date, last_active_date]for each player, avoiding unnecessary padding. - Using
.get(date, 0)handles missing dates cleanly. On a calendar date with no sessions, we add zero to the running total, which keeps it unchanged.
Complexity
Let n be the number of session records, P the number of unique players, D the number of unique (player, date) pairs with activity, and C the number of distinct calendar dates.
-
Time:
- Aggregation: O(n) to populate the nested dictionary.
- Part A: O(D log D) for sorting active dates across all players.
- Part B: O(P · C) in the worst case, if every player's range spans the full calendar.
-
Space: O(D) for the aggregated counts, plus O(P · C) for Part B output in the worst case.
Edge Cases
- Multiple sessions on the same day: Aggregation sums them into a single daily count.
- Player with one active day: First and last dates are identical, so Part B returns a single row matching Part A.
- Calendar contains duplicates or out-of-range dates: We deduplicate and filter to
[first, last]per player. - Players with no sessions: They never appear in the input
rows, so they produce no output. - Dates are ISO strings: Lexicographic comparison works correctly without parsing.
from collections import defaultdict
from typing import Dict, List
def player_activity_totals(rows: List[List], calendar: List[str]) -> Dict[str, List[List]]:
# Build daily counts: player -> date -> count
daily_counts = defaultdict(lambda: defaultdict(int))
for _, player_id, _, session_date in rows:
daily_counts[player_id][session_date] += 1
# Part A: compute running totals over active dates only
part_a = []
for player_id in sorted(daily_counts):
running_total = 0
for date in sorted(daily_counts[player_id]):
count = daily_counts[player_id][date]
running_total += count
part_a.append([player_id, date, count, running_total])
# Part B: densify with calendar dates in each player's range
calendar_set = sorted(set(calendar))
part_b = []
for player_id in sorted(daily_counts):
active_dates = sorted(daily_counts[player_id])
earliest = active_dates[0]
latest = active_dates[-1]
running_total = 0
for date in calendar_set:
if earliest <= date <= latest:
count = daily_counts[player_id].get(date, 0)
running_total += count
part_b.append([player_id, date, count, running_total])
return {"part_a": part_a, "part_b": part_b}