Percentage of time spent sending vs. opening snaps by age bucket

Quick Overview

Write a PostgreSQL query that joins user activity to age buckets and reports, for each bucket, what percentage of combined send and open time went to sending snaps and to opening them. It tests conditional aggregation with CASE inside SUM, filtering rows before grouping, and rounding percentages.

Percentage of time spent sending vs. opening snaps by age bucket

Company: Snapchat

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Each row of `activities` records time a user spent on one action in the app: sending a snap, opening a snap, or chatting. Each user belongs to one age bucket in `age_breakdown`. For each `age_bucket`, return `send_perc` and `open_perc`: the time spent sending snaps and the time spent opening snaps, each as a percentage of the bucket's total send plus open time. Chat time is excluded from both percentages and from the total. Round both percentages to 2 decimal places. ```hint Where chat time goes Decide whether chat rows should reach the aggregation at all. The denominator must count only send and open time. ``` ### Input Table(s) `activities` | Column | Type | Meaning | |---|---|---| | activity_id | INT | One row per activity | | user_id | INT | User who did the activity, not null | | activity_type | VARCHAR(10) | `'send'`, `'open'` or `'chat'`, not null | | time_spent | DECIMAL(10,2) | Time spent on the activity, not null and always positive | | activity_date | DATE | Day of the activity, not null | Primary key: `activity_id`. `age_breakdown` | Column | Type | Meaning | |---|---|---| | user_id | INT | The user | | age_bucket | VARCHAR(10) | Age range label such as `'21-25'`, not null | Primary key: `user_id`. It joins to `activities.user_id`. ### Output Contract | Column | Meaning | |---|---| | age_bucket | The age bucket | | send_perc | 100 * send time / (send time + open time) for the bucket, rounded to 2 decimal places | | open_perc | 100 * open time / (send time + open time) for the bucket, rounded to 2 decimal places | Return one row per age bucket that has at least one send or open activity, ordered by `age_bucket` ascending. ### Example `activities` | activity_id | user_id | activity_type | time_spent | activity_date | |---|---|---|---|---| | 1 | 101 | send | 4.50 | 2022-06-22 | | 2 | 101 | open | 3.00 | 2022-06-22 | | 3 | 102 | chat | 6.00 | 2022-06-23 | | 4 | 102 | open | 2.50 | 2022-06-23 | | 5 | 103 | send | 1.25 | 2022-06-24 | | 6 | 104 | send | 5.00 | 2022-06-25 | | 7 | 104 | open | 2.00 | 2022-06-25 | | 8 | 104 | chat | 9.00 | 2022-06-25 | | 9 | 105 | chat | 3.00 | 2022-06-26 | `age_breakdown` | user_id | age_bucket | |---|---| | 101 | 21-25 | | 102 | 21-25 | | 103 | 26-30 | | 104 | 31-35 | | 105 | 36-40 | Expected result: | age_bucket | send_perc | open_perc | |---|---|---| | 21-25 | 45.00 | 55.00 | | 26-30 | 100.00 | 0.00 | | 31-35 | 71.43 | 28.57 | In `21-25`, send time is 4.50 and open time is 3.00 + 2.50 = 5.50, so the total is 10.00. User 102's 6.00 of chat time does not count. `26-30` has a send and no open, so `open_perc` is 0.00. In `31-35`, 5.00 out of 7.00 is 71.43 after rounding, and user 104's chat time is ignored. `36-40` has only chat time, so it does not appear. ### Constraints and Clarifications - `activities` has at most 10,000 rows. - `time_spent` is always positive, so every bucket in the output has a positive total. - Each user appears exactly once in `age_breakdown`, so the join does not duplicate activity rows. - `age_bucket` values are range labels like `'21-25'`, `'26-30'` and `'31-35'`. Sort them as text. - A bucket with send time but no open time (or the reverse) still appears, with 0.00 for the missing type. - A user can have many activities. All of them count toward the bucket totals, and there is no date filter. - The two percentages are rounded separately, so their sum can differ from 100.00 by 0.01.

Overview: Write a PostgreSQL query that joins user activity to age buckets and reports, for each bucket, what percentage of combined send and open time went to sending snaps and to opening them. It tests conditional aggregation with CASE inside SUM, filtering rows before grouping, and rounding percentages.

Snapchat logo
Snapchat
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of activities records time a user spent on one action in the app: sending a snap, opening a snap, or chatting. Each user belongs to one age bucket in age_breakdown.

For each age_bucket, return send_perc and open_perc: the time spent sending snaps and the time spent opening snaps, each as a percentage of the bucket's total send plus open time. Chat time is excluded from both percentages and from the total. Round both percentages to 2 decimal places.

Input Table(s)

activities

ColumnTypeMeaning
activity_idINTOne row per activity
user_idINTUser who did the activity, not null
activity_typeVARCHAR(10)'send', 'open' or 'chat', not null
time_spentDECIMAL(10,2)Time spent on the activity, not null and always positive
activity_dateDATEDay of the activity, not null

Primary key: activity_id.

age_breakdown

ColumnTypeMeaning
user_idINTThe user
age_bucketVARCHAR(10)Age range label such as '21-25', not null

Primary key: user_id. It joins to activities.user_id.

Output Contract

ColumnMeaning
age_bucketThe age bucket
send_perc100 * send time / (send time + open time) for the bucket, rounded to 2 decimal places
open_perc100 * open time / (send time + open time) for the bucket, rounded to 2 decimal places

Return one row per age bucket that has at least one send or open activity, ordered by age_bucket ascending.

Example

activities

activity_iduser_idactivity_typetime_spentactivity_date
1101send4.502022-06-22
2101open3.002022-06-22
3102chat6.002022-06-23
4102open2.502022-06-23
5103send1.252022-06-24
6104send5.002022-06-25
7104open2.002022-06-25
8104chat9.002022-06-25
9105chat3.002022-06-26

age_breakdown

user_idage_bucket
10121-25
10221-25
10326-30
10431-35
10536-40

Expected result:

age_bucketsend_percopen_perc
21-2545.0055.00
26-30100.000.00
31-3571.4328.57

In 21-25, send time is 4.50 and open time is 3.00 + 2.50 = 5.50, so the total is 10.00. User 102's 6.00 of chat time does not count. 26-30 has a send and no open, so open_perc is 0.00. In 31-35, 5.00 out of 7.00 is 71.43 after rounding, and user 104's chat time is ignored. 36-40 has only chat time, so it does not appear.

Constraints and Clarifications

  • activities has at most 10,000 rows.
  • time_spent is always positive, so every bucket in the output has a positive total.
  • Each user appears exactly once in age_breakdown , so the join does not duplicate activity rows.
  • age_bucket values are range labels like '21-25' , '26-30' and '31-35' . Sort them as text.
  • A bucket with send time but no open time (or the reverse) still appears, with 0.00 for the missing type.
  • A user can have many activities. All of them count toward the bucket totals, and there is no date filter.
  • The two percentages are rounded separately, so their sum can differ from 100.00 by 0.01.