Cohort retention for a language-learning app
A language-learning app built in Berlin logs one line every time a user is active. Product wants a monthly cohort table: of the users who started in a given month, how many were still active one month later, two months later, and so on.
Task: line 1 is the number of events n. Then n lines user YYYY-MM-DD (not sorted; the same user can appear many times a month). A user's cohort is the month (YYYY-MM) of their earliest event. Let the first month in the whole log be M0 and the last month M1 (every calendar month in between counts, even if empty). For each cohort month that has at least one user, in ascending order, print one line COHORT size=S p0 p1 ... pk, where pj is the percentage of the cohort's users with at least one event in the month j months after the cohort month, printed with 1 decimal and a % sign, and the offsets run from 0 up to the number of months between the cohort month and M1. Example line: 2026-02 size=4 100.0% 75.0% 25.0%.
Sample tests
Input
12 u1 2026-01-03 u1 2026-02-10 u1 2026-03-01 u2 2026-01-20 u3 2026-01-25 u3 2026-03-02 u4 2026-02-02 u4 2026-03-15 u5 2026-02-20 u6 2026-03-05 u2 2026-02-28 u4 2026-02-09
Expected output
2026-01 size=3 100.0% 66.7% 66.7% 2026-02 size=2 100.0% 50.0% 2026-03 size=1 100.0%
Input
3 x 2026-05-01 x 2026-05-30 y 2026-05-15
Expected output
2026-05 size=2 100.0%
Your answer is also checked against 2 hidden tests.
Need a hint?
Convert each date to a single month number (year * 12 + month) so that month differences become plain subtraction.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
