KatInterview

Daily Session Starters: Count Users Who Opened the App First

SQL & DataAuto-graded · 18 test cases
MediumAuto-gradedSQL & Datasimulationhash-mapsorting

Last updated

Tests window-function thinking, tie-breaking logic, and grouped aggregation by finding users whose very first recorded action each day was opening the app.

Background

You are working with a stream of telemetry records from a mobile app. Every time a user interacts with the app, a record is written to an events log. Each record carries a precise timestamp, a unique record ID, the action performed, and the device that generated it.

Your task is to figure out, for each calendar day, how many distinct users began that day by opening the app — and break those counts down by the operating system and device model tied to that opening action.

Input Format

You receive a list of event records. Each record is a dictionary with the following fields:

FieldTypeDescription
event_idintGlobally unique record identifier
user_idintIdentifier for the user who triggered the event
event_namestrLabel for the action (e.g. "app_launch", "send_chat", "open_camera")
event_timestampstrISO-format timestamp string ("YYYY-MM-DD HH:MM:SS")
osstrOperating system on the device (e.g. "iOS", "Android")
device_modelstrHardware model name (e.g. "iPhone 15", "Pixel 8")

Task

Implement tally_app_launch_openers(events) that returns a list of dictionaries. Each dictionary represents one (date, os, device_model) combination where at least one user's very first action of the day was "app_launch".

Definitions and Rules

  1. Calendar day: determined by the date portion of event_timestamp (i.e. the YYYY-MM-DD prefix, ignoring the time-of-day).
  2. First event of the day for a user: the record with the earliest event_timestamp among all records sharing the same user_id and calendar day. If two records have identical timestamps, the one with the smaller event_id wins.
  3. Qualifying user: a user whose first event of the day (by the rule above) has event_name == "app_launch". A user who triggers an "app_launch" later in the day but not as their very first event does not qualify for that day.
  4. Output rows: for every (event_date, os, device_model) tuple that has at least one qualifying user, emit a dictionary with keys event_date, os, device_model, and user_count.
    • event_date is an ISO date string ("YYYY-MM-DD").
    • user_count is the number of distinct qualifying users for that combination.
  5. Output order: sort results by event_date ascending, then os ascending, then device_model ascending.
  6. Omit any combination with zero qualifying users — do not emit rows with user_count = 0.

Constraints

  • 0 <= len(events) <= 10^6
  • event_id values are unique across the entire input.
  • user_id is a positive integer.
  • event_timestamp is a valid ISO-format datetime string with no timezone component.
  • os and device_model are non-empty strings.
  • A user may appear on multiple days and across multiple devices; each day is evaluated independently.
  • The input list is not guaranteed to be sorted in any particular order.

Examples

Example 1

Input: tally_app_launch_openers([{'event_id': 101, 'user_id': 1, 'event_name': 'app_launch', 'event_timestamp': '2024-03-01 08:00:00', 'os': 'iOS', 'device_model': 'iPhone 15'}, {'event_id': 102, 'user_id': 1, 'event_name': 'send_chat', 'event_timestamp': '2024-03-01 08:05:00', 'os': 'iOS', 'device_model': 'iPhone 15'}, {'event_id': 103, 'user_id': 2, 'event_name': 'open_camera', 'event_timestamp': '2024-03-01 09:00:00', 'os': 'Android', 'device_model': 'Pixel 8'}, {'event_id': 104, 'user_id': 2, 'event_name': 'app_launch', 'event_timestamp': '2024-03-01 09:10:00', 'os': 'Android', 'device_model': 'Pixel 8'}, {'event_id': 105, 'user_id': 3, 'event_name': 'app_launch', 'event_timestamp': '2024-03-01 07:30:00', 'os': 'iOS', 'device_model': 'iPhone 15'}, {'event_id': 106, 'user_id': 3, 'event_name': 'app_launch', 'event_timestamp': '2024-03-02 10:00:00', 'os': 'iOS', 'device_model': 'iPhone 15'}])

Output: [{'event_date': '2024-03-01', 'os': 'iOS', 'device_model': 'iPhone 15', 'user_count': 2}, {'event_date': '2024-03-02', 'os': 'iOS', 'device_model': 'iPhone 15', 'user_count': 1}]

Example 2

Input: tally_app_launch_openers([{'event_id': 10, 'user_id': 7, 'event_name': 'app_launch', 'event_timestamp': '2024-05-10 12:00:00', 'os': 'Android', 'device_model': 'Pixel 8'}, {'event_id': 9, 'user_id': 7, 'event_name': 'send_chat', 'event_timestamp': '2024-05-10 12:00:00', 'os': 'iOS', 'device_model': 'iPhone 14'}])

Output: []

Hints

Hint 1

Start by finding the single earliest event per (user, day) pair before doing any filtering — applying an event_name filter first changes the question entirely.

Hint 2

To pick the winner among tied timestamps, compare (timestamp, event_id) as a tuple; Python tuple comparison handles the two-level sort in one expression.

Hint 3

Once you have one winning record per (user, day), filter to app_launch winners and use a dictionary keyed on (event_date, os, device_model) to accumulate counts; sorting that dictionary's items at the end handles the required output order.