cswork / libraryOpen practice workspace

Coding questions · Intuit

Compute Cohort Retention Rate

This question evaluates a data scientist's competency in cohort-based retention analysis, time-based data manipulation, and aggregation using Data Manipulation (SQL/Python) techniques.

You are given two tables:

  • users(user_id BIGINT, signup_ts TIMESTAMP) — one row per user.
  • user_activity(user_id BIGINT, activity_ts TIMESTAMP, event_name VARCHAR) — zero or more activity rows per user.

Assume all timestamps are in UTC. Define a user's cohort as the calendar month of signup_ts. A user is retained in month k if they have at least one row in user_activity during the calendar month that is k months after their cohort month. Month 0 is the signup month.

Write a SQL query to compute cohort-based monthly retention for k = 0, 1, 2, 3, 4, 5, 6.

Return these columns:

  • cohort_month — first day of the signup month
  • months_since_signup — integer from 0 to 6
  • cohort_size — number of users in the cohort
  • retained_users — number of distinct users active in that relative month
  • retention_rate — retained_users / cohort_size as a decimal

Make sure each user is counted at most once per cohort-month and relative month.

Compute Cohort Retention Rate

Table users

columntypeconstraints
user_idBIGINT
signup_tsTIMESTAMP

Sample data:

signup_tsuser_id
2024-01-05 10:00:001
2024-01-20 15:30:002
2024-02-10 08:00:003
2024-03-01 00:00:004

Table user_activity

columntypeconstraints
user_idBIGINT
activity_tsTIMESTAMP
event_nameVARCHAR

Sample data:

activity_tsevent_nameuser_id
2024-01-10 09:00:00open1
2024-02-12 12:00:00open1
2024-04-01 10:00:00purchase1
2024-01-25 18:45:00open2
2024-03-05 11:20:00open2
2024-03-20 16:05:00purchase2
2024-02-28 14:10:00open3
2024-04-02 09:30:00open3
2024-03-15 07:00:00open4
2024-04-01 13:00:00open4
2024-09-05 08:00:00open4

Expected output

One row per (cohort_month, months_since_signup in 0..6). cohort_month is the first day of the signup month (UTC). cohort_size is the total users who signed up in that cohort. retained_users counts distinct users from that cohort with at least one activity in the month that is k months after the cohort month (users counted at most once per cohort-month and k). retention_rate is retained_users divided by cohort_size.

cohort_monthcohort_sizemonths_since_signupretained_usersretention_rate
2024-01-012021
2024-01-012110.5
2024-01-012210.5
2024-01-012310.5
2024-01-012400
2024-01-012500
2024-01-012600
2024-02-011011
2024-02-011100
2024-02-011211
2024-02-011300
2024-02-011400
2024-02-011500
2024-02-011600
2024-03-011011
2024-03-011111
2024-03-011200
2024-03-011300
2024-03-011400
2024-03-011500
2024-03-011611

Hints

  1. Derive cohort_month as the first day of the signup month using date_trunc('month', signup_ts).
  2. Generate months_since_signup values 0..6 with generate_series and cross join to all cohorts.
  3. Deduplicate user_activity to one row per user per activity month before joining to avoid double counting.
  4. Join on activity_month = cohort_month + k months; count distinct users per (cohort_month, k).