Monthly Retention via Self-Merge
Problem
**[Asked at Meta and Uber]**
For each month, calculate what percentage of that month's active users were
**also active in the previous month** (retained users).
Return `month` (YYYY-MM), `total_users`, `retained_users`, and `retention_pct`
(rounded to 2 dp), ordered by month.
Input data
Example rows — the live problem includes the full dataset.
activity
| user_id | activity_date |
|---|---|
| 101 | 2023-01-05 |
| 102 | 2023-01-10 |
| 103 | 2023-01-15 |
| 101 | 2023-02-05 |
| 104 | 2023-02-10 |
Expected output
Your answer should return 3 rows with the columns month, total_users, retained_users, retention_pct.
Starter code (Pandas (Python))
import pandas as pd
def monthly_retention(activity: pd.DataFrame) -> pd.DataFrame:
# Your code here
return activitySolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock