KatInterview

Player Activity Running Totals with Date Gaps

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Datawindow-functionsaggregationdate-handling

Last 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] where session_date is 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.