KatInterview

Daily and Rolling Active User Counts with Zero-Fill

Arrays & HashingAuto-graded · 14 test cases
EasyAuto-gradedArrays & Hashinghash-mapsliding-windowdate-manipulation

Last updated

Tests building a complete calendar spine, counting distinct daily users, and maintaining a 30-day rolling active-user window.

You are given a date range and a list of login event records. Each record is a dictionary with two keys:

  • user_id (int): the identifier of the person who logged in.
  • login_ts (str): an ISO-8601 timestamp (e.g. "2023-01-03T09:00:00Z"). The calendar day is determined by the date portion of this timestamp.

Define two metrics for every calendar day d in the inclusive range [start_date, end_date]:

  • DAU(d) — the number of distinct user_id values whose login timestamp falls on calendar day d. If a user logs in multiple times on the same day (even from different browsers), they are counted once.
  • MAU_l30d(d) — the number of distinct user_id values who logged in at any point during the 30-day window [d − 29 days, d], inclusive of both endpoints. A user active on several days within the window is counted only once.

Return a list of dictionaries, one per calendar day in ascending order, each containing:

  • "date": the day formatted as "YYYY-MM-DD".
  • "dau": the DAU value for that day.
  • "mau_l30d": the rolling 30-day MAU value for that day.

Important rules:

  • The output must include every calendar day in the range, even days with zero login activity (dau = 0). Do not skip days that have no records.
  • Login records whose timestamp falls before start_date still contribute to MAU windows for days inside the range (because the 30-day lookback can reach earlier dates). Login records after end_date are irrelevant and can be ignored.
  • If start_date is later than end_date, return an empty list.
  • If the login list is empty, every day in the range has dau = 0 and mau_l30d = 0.

Constraints:

  • start_date and end_date are valid date strings in "YYYY-MM-DD" format.
  • The date range spans at most 366 days.
  • logins has at most 100,000 records.
  • user_id values are positive integers.
  • login_ts strings are valid ISO-8601 timestamps, possibly suffixed with Z for UTC.

Examples

Example 1

Input: active_user_metrics('2023-01-01', '2023-01-04', [{'user_id': 1, 'login_ts': '2023-01-01T10:00:00Z'}, {'user_id': 1, 'login_ts': '2023-01-01T18:00:00Z'}, {'user_id': 2, 'login_ts': '2023-01-03T09:00:00Z'}])

Output: [{'date': '2023-01-01', 'dau': 1, 'mau_l30d': 1}, {'date': '2023-01-02', 'dau': 0, 'mau_l30d': 1}, {'date': '2023-01-03', 'dau': 1, 'mau_l30d': 2}, {'date': '2023-01-04', 'dau': 0, 'mau_l30d': 2}]

Hints

Hint 1

Build a dictionary mapping each calendar day to a set of distinct user_ids who logged in that day. Using a set automatically deduplicates repeat logins.

Hint 2

Iterate over every date in the range from start to end so that zero-activity days are naturally included with dau = 0. Do not iterate over the logins themselves.

Hint 3

For each output day, union the per-day user sets across the 30-day lookback window [d-29, d] to compute distinct MAU. Do not sum the daily DAU values, as that would double-count users active on multiple days.