KatInterview

Daily Active Users, New-User Cohorts and Next-Day Return Rate

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datacohort-analysisretentiondeduplication

Last updated

Build a fixed 7-day report of active users, first-activity cohorts, next-day return rate and revenue per active user from raw logs, with bot filtering and deduplication.

You are given three raw tables as lists of rows, each row a list of strings (or None for missing values). Build a daily report for the fixed reporting window 2025-08-26 through 2025-09-01 (7 consecutive days, all times in UTC).

Input

  • accounts: rows [user_id, signup_date, country]. This table is provided for completeness and is not needed for any metric.
  • activity: rows [user_id, event_time, event_type, product_id]. event_time looks like "2025-08-26 09:00:00", and product_id may be None.
  • orders: rows [user_id, purchase_time, order_id, amount]. purchase_time has the same format as event_time, and amount is a decimal string such as "9.99".

Preparation rules

  1. Bots. Ignore every row whose user_id starts with the literal prefix bot_ (case-sensitive, the underscore is a normal character). Apply this to activity and orders, so bots affect no metric.
  2. Deduplication. In activity, rows that are identical in all four fields count as a single event. Do not deduplicate orders.
  3. Calendar day. The day of a timestamp is its first 10 characters (YYYY-MM-DD), interpreted as a UTC date.

Metrics per day d in the window

  • dau: number of distinct users with at least one (cleaned) event on day d.
  • new_users: number of users whose earliest event day over the entire activity log (not just inside the window) is exactly d. This depends only on events, never on signup_date.
  • d1_retention_rate: among the new_users of day d, the fraction that also have at least one event on day d + 1. The day after the window's last day (2025-09-02) is checked normally if events exist. If new_users is 0, the rate is 0.0.
  • revenue_per_dau: total amount of non-bot purchases whose purchase day is d, divided by dau. If dau is 0, the value is 0.0, even if revenue exists that day. Purchases are counted by their own timestamp and need not be tied to that user's activity.

Output

Return a list of exactly 7 dictionaries, ordered by day ascending, one for each day in the window (including days with no activity). Each dictionary has the keys:

  • "day": the ISO date string, e.g. "2025-08-26"
  • "dau": int
  • "new_users": int
  • "d1_retention_rate": number
  • "revenue_per_dau": number

Do not round the ratios.

Constraints

  • The window is fixed: 2025-08-26 to 2025-09-01 inclusive.
  • Row counts can be large enough that a single pass with hash-based sets is expected (no per-day rescans of all events).
  • Timestamps are well-formed strings of the form YYYY-MM-DD HH:MM:SS.
  • amount values are non-negative decimal strings.
  • Events and purchases may fall outside the window. Events before the window matter for deciding who is "new". Purchases outside the window are ignored.
  • User ids such as botx9 (no underscore after bot) are not bots.

Examples

Example 1

Input: daily_cohort_report([['u1', '2025-08-25', 'US'], ['u2', '2025-08-26', 'US'], ['u3', '2025-08-26', 'IN'], ['u4', '2025-08-31', 'US'], ['u5', '2025-09-01', 'US'], ['bot_1', '2025-08-26', 'US']], [['u1', '2025-08-26 09:00:00', 'app_open', None], ['u1', '2025-08-27 10:00:00', 'app_open', None], ['u2', '2025-08-26 12:00:00', 'app_open', None], ['u2', '2025-08-27 13:00:00', 'view', 'P1'], ['u2', '2025-08-28 12:00:00', 'app_open', None], ['u3', '2025-08-26 15:00:00', 'app_open', None], ['u3', '2025-08-27 16:00:00', 'app_open', None], ['u4', '2025-09-01 08:00:00', 'app_open', None], ['u5', '2025-09-01 09:00:00', 'app_open', None], ['bot_1', '2025-08-26 10:00:00', 'app_open', None]], [['u1', '2025-08-27 11:00:00', 'o1', '9.99'], ['u2', '2025-08-28 11:30:00', 'o2', '4.00'], ['u3', '2025-08-29 14:00:00', 'o3', '2.50'], ['u4', '2025-09-01 09:30:00', 'o4', '1.00'], ['u5', '2025-09-02 10:00:00', 'o5', '5.00'], ['bot_1', '2025-08-26 10:30:00', 'o6', '100.00']])

Output: [{'day': '2025-08-26', 'dau': 3, 'new_users': 3, 'd1_retention_rate': 1, 'revenue_per_dau': 0}, {'day': '2025-08-27', 'dau': 3, 'new_users': 0, 'd1_retention_rate': 0, 'revenue_per_dau': 3.33}, {'day': '2025-08-28', 'dau': 1, 'new_users': 0, 'd1_retention_rate': 0, 'revenue_per_dau': 4}, {'day': '2025-08-29', 'dau': 0, 'new_users': 0, 'd1_retention_rate': 0, 'revenue_per_dau': 0}, {'day': '2025-08-30', 'dau': 0, 'new_users': 0, 'd1_retention_rate': 0, 'revenue_per_dau': 0}, {'day': '2025-08-31', 'dau': 0, 'new_users': 0, 'd1_retention_rate': 0, 'revenue_per_dau': 0}, {'day': '2025-09-01', 'dau': 2, 'new_users': 2, 'd1_retention_rate': 0, 'revenue_per_dau': 0.5}]

Hints

Hint 1

Reduce the cleaned events to a set of distinct (user, day) pairs first. DAU, first activity day and retention can all be derived from it.

Hint 2

A user's cohort day is the minimum day over all their events, including those before the window. Only users whose cohort day falls in the window count as new.

Hint 3

For retention, intersect the cohort set of day d with the set of active users on day d+1. Handle zero denominators explicitly by returning 0.0.