KatInterview

Day-30 Cohort Retention from Signup and Activity Logs

SQL & DataAuto-graded · 18 test cases
MediumAuto-gradedSQL & Datahash-setdate-arithmeticaggregation

Last updated

Group users into signup-date cohorts and count who was active exactly 30 days later, using sets and date arithmetic with careful handling of duplicates.

Day-30 Retention by Signup Cohort

You have two logs from a personal-finance app, each given as a list of dictionaries:

  • signup_records: each item has "user_id" and "signup_date". Dates are ISO strings ("YYYY-MM-DD").
  • activity_records: each item has "user_id" and "activity_date", also ISO date strings.

A cohort is the group of users who share the same signup date. A user is retained if they have at least one activity record dated exactly 30 calendar days after their signup date. Activity on day 29, day 31, or any other day does not count.

Return one summary row per cohort.

Output format

Return a list of dictionaries, one per distinct signup date, sorted by signup_date ascending. Each dictionary has these keys:

  • "signup_date": the cohort's date string, exactly as given in the input.
  • "cohort_size": the number of distinct users in that cohort.
  • "retained_users": the number of distinct users in that cohort who were active exactly 30 days after signup.
  • "retention_rate": retained_users / cohort_size as a plain fraction (for example 0.5, not 50). Do not round it and do not multiply by 100.

Rules

  • A user counts at most once per cohort, even if signup_records lists the same user and date several times.
  • Several activity rows for the same user on the same day count as a single retained user.
  • Activity rows for users who do not appear in signup_records are ignored.
  • A cohort with no retained users still appears in the output, with retained_users equal to 0 and retention_rate equal to 0.0.
  • If there are no signup records, return an empty list.
  • Compute "30 days later" with real calendar arithmetic, so month and year boundaries and leap years are handled correctly.

Constraints

  • user_id values are strings. Compare them as given.
  • All dates are valid ISO YYYY-MM-DD strings.
  • The input lists may be empty.
  • Every cohort in the output has at least one user.

Examples

Example 1

Input: compute_day30_retention([{'user_id': '1', 'signup_date': '2023-01-01'}, {'user_id': '2', 'signup_date': '2023-01-01'}], [{'user_id': '1', 'activity_date': '2023-01-31'}])

Output: [{'signup_date': '2023-01-01', 'cohort_size': 2, 'retained_users': 1, 'retention_rate': 0.5}]

Example 2

Input: compute_day30_retention([{'user_id': '1', 'signup_date': '2023-01-01'}, {'user_id': '2', 'signup_date': '2023-01-01'}, {'user_id': '3', 'signup_date': '2023-01-02'}], [{'user_id': '1', 'activity_date': '2023-01-31'}, {'user_id': '1', 'activity_date': '2023-01-31'}, {'user_id': '3', 'activity_date': '2023-02-01'}])

Output: [{'signup_date': '2023-01-01', 'cohort_size': 2, 'retained_users': 1, 'retention_rate': 0.5}, {'signup_date': '2023-01-02', 'cohort_size': 1, 'retained_users': 1, 'retention_rate': 1}]

Hints

Hint 1

Think of each cohort as the set of distinct user ids that signed up on that date. Using a set removes duplicate signup rows for free.

Hint 2

Put all (user_id, activity_date) pairs into a set so that checking whether a user was active on one specific date takes constant time, and repeated activity rows collapse automatically.

Hint 3

For each cohort, parse the signup date, add 30 days with a timedelta, convert back to an ISO string, and count the cohort users whose (user_id, target_date) pair is in the activity set. Remember to sort cohorts by date.