← Problems2. Rolling 7-day active usersMediumSQL
00:00 / 20:00

Rolling 7-day active users

Medium50% solve itAsked at Amazon

Return the number of distinct users active in each trailing 7-day window, one row per day.

Input schema

activity user_id bigint active_at timestamp

Constraints

  • The window is 7 days inclusive: the row for day D counts activity from D - 6 through D
  • One row per day from the first day of activity to the last, with no gaps — a day with no activity in its window returns 0, not a missing row
  • A user active several times inside one window counts once
  • day is formatted YYYY-MM-DD
  • Order by day ascending

Example 1the window reaches back six days

Input · activity
user_idactive_at
12024-01-01 10:00:00
22024-01-03 10:00:00
Output
dayactive_users
2024-01-011
2024-01-021
2024-01-032

Example 2a user active twice in a window counts once

Input · activity
user_idactive_at
52024-02-01 08:00:00
52024-02-01 20:00:00
52024-02-02 09:00:00
Output
dayactive_users
2024-02-011
2024-02-021

Topics

window functions

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.sql
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.