Count Users Whose First Daily Event Is App Launch
Company: Snapchat
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Technical Screen
Given the PostgreSQL table:
```sql
app_events (
event_id BIGINT PRIMARY KEY,
user_id BIGINT NOT NULL,
event_ts TIMESTAMP NOT NULL,
event_name TEXT NOT NULL,
os_type TEXT NOT NULL,
device_type TEXT NOT NULL
)
```
Write one read-only `SELECT`/CTE query that returns, for each calendar date, `os_type`, and `device_type`, the number of distinct users whose first recorded event on that date is `app_open`.
Return exactly:
```text
event_date, os_type, device_type, user_count
```
### Constraints and Clarifications
- Derive `event_date` as `event_ts::date`.
- “First” is per user and calendar date across every event name, not merely the first `app_open` row.
- Break equal timestamps by smaller `event_id`.
- Attribute a qualifying user to the OS and device on that first event.
- Omit groups with zero qualifying users and sort by all three grouping columns ascending.
```hint Rank before filtering
Identify one first row per user-date over the complete event stream, then retain rows whose event name is `app_open`.
```
### Evaluation Focus
- Correct partitioning and deterministic ordering.
- Filtering after, rather than before, selecting the first daily event.
- Distinct-user counting without double counting.
- Valid PostgreSQL date handling and exact output columns.
### Extension
How would the query change if “day” were defined in each user's local time zone?
Overview: Write PostgreSQL to count users whose first event of each day is an app launch, grouped by operating system and device. Practice deterministic window ranking and date-level aggregation.
Read the full Snapchat Data Scientist interview experience this question came from
Given app_events, write one read-only SELECT/CTE query that returns for each event_ts::date, os_type, and device_type the number of distinct users whose first recorded event on that calendar date is app_open. Select the first event per user-date across every event name, break equal timestamps by smaller event_id, attribute the user to the OS and device on that first event, omit zero-count groups, and sort by event_date, os_type, and device_type ascending. Return exactly event_date, os_type, device_type, user_count.
Tables
app_events(event_id BIGINT, user_id BIGINT, event_ts TIMESTAMP, event_name TEXT, os_type TEXT, device_type TEXT)
Hints
- Rank before filtering: identify one first row per user-date over the complete event stream, then retain rows whose event name is app_open.