Moving Average of Daily Active Users
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Given a log of user activity, compute daily active users (DAU) for each date, meaning the number of distinct `user_id` values with at least one activity on that date. Alongside it, compute the 7-day trailing moving average of DAU: for each date, the average DAU over that date and the 6 dates before it that appear in the data. When fewer than 6 earlier dates exist, for example at the start of the data, average over all the dates available.
```hint Order of operations
Get to exactly one row per date before you average anything.
```
### Input Table(s)
Table `user_activity`:
| Column | Type | Meaning |
|---|---|---|
| id | INT | Row identifier |
| user_id | INT | User who performed the activity |
| activity_date | DATE | Calendar date of the activity |
| activity_type | VARCHAR(50) | Kind of activity, such as `login`, `click` or `purchase` |
Keys: `id` is the primary key. Nothing makes `(user_id, activity_date)` unique, so a user can have many rows on the same date.
### Output Contract
- Columns, in this order: `activity_date`, `dau`, `7_day_moving_avg`.
- One row per date that has at least one row in `user_activity`.
- `dau` is the count of distinct `user_id` on that date.
- `7_day_moving_avg` is the average of `dau` over the current output row and up to 6 output rows immediately before it in `activity_date` order, rounded to 2 decimal places.
- Order by `activity_date` ascending. Each date appears once, so this order is unique.
### Example
Input `user_activity`:
| id | user_id | activity_date | activity_type |
|---|---|---|---|
| 1 | 10 | 2024-03-01 | login |
| 2 | 10 | 2024-03-01 | click |
| 3 | 10 | 2024-03-01 | purchase |
| 4 | 20 | 2024-03-02 | login |
| 5 | 30 | 2024-03-02 | login |
| 6 | 10 | 2024-03-03 | login |
| 7 | 20 | 2024-03-03 | login |
| 8 | 30 | 2024-03-03 | click |
| 9 | 40 | 2024-03-03 | login |
Expected output:
| activity_date | dau | 7_day_moving_avg |
|---|---|---|
| 2024-03-01 | 1 | 1.00 |
| 2024-03-02 | 2 | 1.50 |
| 2024-03-03 | 4 | 2.33 |
On 2024-03-01 only user 10 is active, so the three rows count as one user. On 2024-03-02 users 20 and 30 are active, and on 2024-03-03 users 10, 20, 30 and 40. Fewer than 7 dates of data exist, so the averages are 1 / 1 = 1.00, (1 + 2) / 2 = 1.50 and (1 + 2 + 4) / 3 = 2.333..., which rounds to 2.33.
### Constraints and Clarifications
- `user_activity` has at most 10,000 rows.
- No column is NULL. `user_id` is a positive integer, `activity_date` is a valid date and `activity_type` is non-empty.
- Every row counts as activity, whatever its `activity_type`.
- A user with several rows on one date counts once for that date. A user active on several dates counts on each of them.
- The window counts dates that appear in the data. A date with no activity has no output row and is never part of a window, so a gap in the dates does not shrink the window: it still holds the current date and the 6 most recent earlier dates with activity.
- The column name `7_day_moving_avg` starts with a digit, so in PostgreSQL it has to be written as a quoted identifier.
Overview: Compute daily active users from an activity log, then a 7-day trailing moving average of that daily count, rounded to 2 decimals. The question tests counting distinct users per day, choosing the right window function frame over daily totals, and producing a deterministic ordered result.