Count Distinct Active Users in the Inclusive Thirty-Day Window

Quick Overview

Count Distinct Active Users in the Inclusive Thirty-Day Window. Report day and active_users for dates in the 30-day period ending 2019-07-27 inclusive. A user is active after any activity of the listed types on that date. Count each user once.

Count Distinct Active Users in the Inclusive Thirty-Day Window

Company: Amazon

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem Report day and active_users for dates in the 30-day period ending 2019-07-27 inclusive. A user is active after any activity of the listed types on that date. Count each user once per day, retain duplicate source rows without overcounting, and omit zero-active-user dates. Row order is unrestricted. ### Tables Table: `Activity` ```text +---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | session_id | int | | activity_date | date | | activity_type | enum | +---------------+---------+ This table may have duplicate rows. The activity_type column is an ENUM (category) of type ('open_session', 'end_session', 'scroll_down', 'send_message'). The table shows the user activities for a social media website. Note that each session belongs to exactly one user. ``` ### Constraints - In this version, `user_id` is non-NULL in every activity row. - In this version, `activity_type` is non-NULL and is one of `open_session`, `end_session`, `scroll_down`, or `send_message`. ### Example ```text Activity (user_id,session_id,activity_date,activity_type): (1,1,2019-06-27,open_session), (1,2,2019-06-28,open_session), (1,2,2019-06-28,scroll_down), (2,3,2019-07-27,end_session), (3,4,2019-07-28,open_session) Output (day,active_users): (2019-06-28,1), (2019-07-27,1) ``` ### What a Strong Answer Covers - Use all 30 calendar dates in the specified window, including both endpoints. - Count a user only once per date and omit days with no activity. ### Follow-up Questions - How would you show dates with zero active users? - How would you calculate daily counts over a window that changes with the current date?

Overview: Count Distinct Active Users in the Inclusive Thirty-Day Window. Report day and active_users for dates in the 30-day period ending 2019-07-27 inclusive. A user is active after any activity of the listed types on that date. Count each user once.

Amazon logo
Amazon
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Report day and active_users for dates in the 30-day period ending 2019-07-27 inclusive. A user is active after any activity of the listed types on that date. Count each user once per day, retain duplicate source rows without overcounting, and omit zero-active-user dates. Row order is unrestricted.

Tables

Table: Activity

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user_id       | int     |
| session_id    | int     |
| activity_date | date    |
| activity_type | enum    |
+---------------+---------+
This table may have duplicate rows.
The activity_type column is an ENUM (category) of type ('open_session', 'end_session', 'scroll_down', 'send_message').
The table shows the user activities for a social media website.
Note that each session belongs to exactly one user.

Constraints

  • In this version, user_id is non-NULL in every activity row.
  • In this version, activity_type is non-NULL and is one of open_session , end_session , scroll_down , or send_message .

Example

Activity (user_id,session_id,activity_date,activity_type): (1,1,2019-06-27,open_session), (1,2,2019-06-28,open_session), (1,2,2019-06-28,scroll_down), (2,3,2019-07-27,end_session), (3,4,2019-07-28,open_session)
Output (day,active_users): (2019-06-28,1), (2019-07-27,1)

What a Strong Answer Covers Guidance

  • Use all 30 calendar dates in the specified window, including both endpoints.
  • Count a user only once per date and omit days with no activity.

Follow-up Questions Guidance

  • How would you show dates with zero active users?
  • How would you calculate daily counts over a window that changes with the current date?