KatInterview

Cohort Return Rates by Months Since First Visit

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashinghash-mapgroupingdates

Last updated

Group users by the month of their first activity, then compute the fraction of each cohort that was active in every later month offset, using calendar-month arithmetic.

Cohort Return Rates by Months Since First Visit

You are given a log of user visits. Each entry is a pair [user_id, visit_date], where visit_date is a string in YYYY-MM-DD format. A user may appear many times, including several times on the same day or in the same month.

Build a cohort retention table as follows.

  • A user's cohort is the calendar month (formatted YYYY-MM) of their earliest visit.

  • For any month in which a user visited, its month offset is the number of calendar months between that month and the user's cohort. The cohort month itself has offset 0, the following month has offset 1, and so on. Count calendar months, not days: January 31 and February 1 are one month apart, and December 2023 to January 2024 is one month apart.

  • For a given (cohort, offset) pair, the retention rate is:

    (number of distinct users in that cohort who visited at least once during the month at that offset) / (total number of distinct users in that cohort)

    Each user counts at most once per month, no matter how many visits they made in it.

Return a list of dictionaries, one per (cohort, offset) pair in which at least one cohort member visited. Pairs with no returning users are omitted, so there are no rows with a rate of zero. Each dictionary has exactly these keys:

  • "cohort_month": the cohort as a YYYY-MM string
  • "month_number": the integer offset
  • "retention_rate": the rate as a plain fraction between 0 and 1, computed by ordinary division. Do not round it and do not multiply it by 100.

Sort the result by cohort_month ascending (as a string, which matches chronological order), and then by month_number ascending. If the log is empty, return an empty list.

Constraints

  • Every date is a valid string of the form YYYY-MM-DD.
  • Dates span at most about two years.
  • Every user_id is hashable (for example an integer), and users are distinguished by their user_id value.
  • The log may be empty.
  • Because of the omission rule, every cohort always has a row with month_number = 0 and a rate of exactly 1.

Examples

Example 1

Input: cohort_retention_table([[1, '2023-01-05'], [1, '2023-02-10'], [2, '2023-01-20'], [3, '2023-02-01']])

Output: [{'cohort_month': '2023-01', 'month_number': 0, 'retention_rate': 1}, {'cohort_month': '2023-01', 'month_number': 1, 'retention_rate': 0.5}, {'cohort_month': '2023-02', 'month_number': 0, 'retention_rate': 1}]

Hints

Hint 1

For each user, you only care about the set of distinct `YYYY-MM` months in which they were active. Slicing the first 7 characters of the date gives you that.

Hint 2

The earliest month of a user's set is their cohort. Convert `YYYY-MM` strings to integers to get the offset as (year difference) * 12 + (month difference), instead of dividing days by 30.

Hint 3

Keep a count of users per cohort and, for every (cohort, offset) pair, a set of user ids that were active. The rate is the size of that set divided by the cohort size. Finally sort the pairs.