Fix the monthly active users count
Your task
An agent wrote a 40-line SQL query for monthly active users, a number that goes to the board. The numbers look plausible, but nobody on the team can follow the joins. Before trusting it, you compute the same metric a second, simpler way, straight from the raw events, and compare.
An assistant wrote distinct_active_users(event_rows) as that second count. It is in the editor. It has a bug. Find it and fix it.
Input and output:
event_rows: one event per row, writtenuser_id|date, with the date asYYYY-MM-DD.- Return one
YYYY-MM=countstring per month that has an active user, sorted by month.
Rules the tests check:
- Split each row at its first
|. Skip a row with no|or with an empty user id. - The month is the first 7 characters of the date: 4 digits,
-, then 2 digits from01to12. Skip a row whose date does not start that way. The rest of the date is not checked. - A month's count is the number of different users with at least one event in it. A user counts once per month, however many rows they have in that month.
- User ids compare exactly: case and spaces matter.
Examples
distinct_active_users(event_rows: list[str]) -> list[str]Example 1
Input
distinct_active_users(["u1|2026-01-15", "u2|2026-01-20", "u1|2026-01-31"])Output
["2026-01=2"]Example 2
Input
distinct_active_users(["u1|2026-01-15", "u1|2026-01-15"])Output
["2026-01=1"]Example 3
Input
distinct_active_users(["u1|2026-01-31", "u1|2026-02-01"])Output
["2026-01=1", "2026-02=1"]