KatInterview

Weekly Cohort Activity Retention Analysis

SQL & DataAuto-graded · 20 test cases
MediumAuto-gradedSQL & Datadata-analysiscohort-analysisretention

Last updated

Group users by signup week, compute seven-day activity retention rates, and handle zero-retention cohorts correctly.

Problem

You are analyzing user activity data stored in two tables:

users table:

  • user_id: unique identifier for each user
  • signup_date: timestamp when the user signed up
  • plan_type: subscription plan type

events table:

  • user_id: identifier linking the event to a user
  • event_ts: timestamp when the event occurred

Your task is to compute retention metrics grouped by signup week cohorts.

Cohort Definition

A cohort is defined by the Monday-midnight start of the calendar week containing each user's signup_date. In this problem, weeks begin on Monday at 00:00:00. For example, any signup between Monday, January 1, 2024 00:00:00 and Sunday, January 7, 2024 23:59:59 belongs to the cohort with signup_week of 2024-01-01 00:00:00.

Retention Definition

A user is considered retained if they have at least one event in the time window starting at their signup_date (inclusive) and ending strictly before signup_date + 7 days. An event at exactly the signup timestamp counts as retained. An event at exactly seven days after signup does not count.

Requirements

Implement a function that takes two lists of dictionaries (representing the tables) and returns one dictionary per cohort with these fields, in this order:

  • signup_week: the Monday-midnight start timestamp of the cohort, formatted as 'YYYY-MM-DD HH:MM:SS', or None for users with null signup dates
  • cohort_size: total number of distinct users in that cohort
  • retained_users: number of distinct users in that cohort who had at least one qualifying event
  • retention_rate: retained_users / cohort_size, rounded to exactly 4 decimal places

Important Rules

  • The cohort size includes all users in that week, even those with zero qualifying events
  • A user with multiple qualifying events counts only once in retained_users
  • Include cohorts where no users were retained (retention_rate = 0.0)
  • Only include cohorts that actually have users; do not add empty calendar weeks
  • Sort results by signup_week ascending, with None cohorts last
  • Users with None signup dates form a separate cohort but can never be retained (comparisons with None are always false)
  • Duplicate user_id entries: take the first occurrence only

Constraints

  • 0 ≤ len(users) ≤ 100,000
  • 0 ≤ len(events) ≤ 1,000,000
  • Timestamps may be provided as datetime objects, date objects, or ISO 8601 strings (with or without 'Z' suffix)
  • All timestamps should be treated as naive (timezone-unaware) for comparison purposes
  • Round retention rates using ROUND_HALF_UP to 4 decimal places

Examples

Example 1

Input: compute_cohort_retention([{'user_id': 1, 'signup_date': '2024-01-01 10:00:00', 'plan_type': 'free'}, {'user_id': 2, 'signup_date': '2024-01-02 12:00:00', 'plan_type': 'pro'}, {'user_id': 3, 'signup_date': '2024-01-08 00:00:00', 'plan_type': 'free'}], [{'user_id': 1, 'event_ts': '2024-01-01 10:00:00'}, {'user_id': 1, 'event_ts': '2024-01-08 10:00:00'}, {'user_id': 2, 'event_ts': '2024-01-09 12:00:00'}, {'user_id': 3, 'event_ts': '2024-01-14 23:59:59'}])

Output: [{'signup_week': '2024-01-01 00:00:00', 'cohort_size': 2, 'retained_users': 1, 'retention_rate': 0.5}, {'signup_week': '2024-01-08 00:00:00', 'cohort_size': 1, 'retained_users': 1, 'retention_rate': 1}]

Example 2

Input: compute_cohort_retention([{'user_id': 10, 'signup_date': '2024-03-04', 'plan_type': 'free'}, {'user_id': 11, 'signup_date': '2024-03-05', 'plan_type': 'free'}, {'user_id': 12, 'signup_date': '2024-03-10', 'plan_type': 'pro'}], [])

Output: [{'signup_week': '2024-03-04 00:00:00', 'cohort_size': 3, 'retained_users': 0, 'retention_rate': 0}]

Hints

Hint 1

Start by grouping users into cohorts based on their signup week, then check retention for each user individually before aggregating.

Hint 2

Use a half-open interval [signup, signup + 7 days) to test whether each event qualifies. Be careful with boundary conditions.

Hint 3

Avoid joining users to events in a way that creates multiple rows per user, which would inflate your counts. Consider computing a boolean 'is_retained' flag per user first.