Snaps Analysis - Problem
Social Media Activity Analysis: Snap Usage by Age Groups
You're working as a data analyst for a social media platform that allows users to send and view snaps (short messages/photos). Your task is to analyze user engagement patterns across different age demographics.
Given two tables:
Activities Table
| Column Name | Type |
|---|---|
| activity_id | int |
| user_id | int |
| activity_type | enum ('send', 'open') |
| time_spent | decimal |
Age Table
| Column Name | Type |
|---|---|
| user_id | int |
| age_bucket | enum ('21-25', '26-30', '31-35') |
Goal: Calculate what percentage of total time each age group spends on sending vs opening snaps. This helps understand behavioral differences between age demographics.
Output Requirements:
- Show age group, activity type, and percentage of total time
- Round percentages to 2 decimal places
- Results can be in any order
Input & Output
Basic Example
$
Input:
{
"activities": [
{"activity_id": 1, "user_id": 101, "activity_type": "send", "time_spent": 5.5},
{"activity_id": 2, "user_id": 101, "activity_type": "open", "time_spent": 3.2},
{"activity_id": 3, "user_id": 102, "activity_type": "send", "time_spent": 4.1},
{"activity_id": 4, "user_id": 103, "activity_type": "open", "time_spent": 2.8}
],
"age": [
{"user_id": 101, "age_bucket": "21-25"},
{"user_id": 102, "age_bucket": "26-30"},
{"user_id": 103, "age_bucket": "21-25"}
]
}
โบ
Output:
[
{"age_bucket": "21-25", "activity_type": "open", "percentage": 35.48},
{"age_bucket": "21-25", "activity_type": "send", "percentage": 35.48},
{"age_bucket": "26-30", "activity_type": "send", "percentage": 26.45}
]
๐ก Note:
Total time = 5.5 + 3.2 + 4.1 + 2.8 = 15.6. Age group 21-25: send=5.5 (35.26%), open=2.8+3.2=6.0 (38.46%). Age group 26-30: send=4.1 (26.28%). Percentages are rounded to 2 decimal places.
Multiple Users Per Age Group
$
Input:
{
"activities": [
{"activity_id": 1, "user_id": 201, "activity_type": "send", "time_spent": 10.0},
{"activity_id": 2, "user_id": 202, "activity_type": "send", "time_spent": 15.0},
{"activity_id": 3, "user_id": 203, "activity_type": "open", "time_spent": 5.0},
{"activity_id": 4, "user_id": 201, "activity_type": "open", "time_spent": 20.0}
],
"age": [
{"user_id": 201, "age_bucket": "21-25"},
{"user_id": 202, "age_bucket": "21-25"},
{"user_id": 203, "age_bucket": "26-30"}
]
}
โบ
Output:
[
{"age_bucket": "21-25", "activity_type": "open", "percentage": 40.0},
{"age_bucket": "21-25", "activity_type": "send", "percentage": 50.0},
{"age_bucket": "26-30", "activity_type": "open", "percentage": 10.0}
]
๐ก Note:
Total time = 50.0. Age group 21-25: send=25.0 (50%), open=20.0 (40%). Age group 26-30: open=5.0 (10%). Shows how multiple users in the same age group are aggregated together.
All Age Groups Present
$
Input:
{
"activities": [
{"activity_id": 1, "user_id": 301, "activity_type": "send", "time_spent": 12.5},
{"activity_id": 2, "user_id": 302, "activity_type": "open", "time_spent": 7.3},
{"activity_id": 3, "user_id": 303, "activity_type": "send", "time_spent": 15.2},
{"activity_id": 4, "user_id": 303, "activity_type": "open", "time_spent": 8.1},
{"activity_id": 5, "user_id": 304, "activity_type": "send", "time_spent": 6.9}
],
"age": [
{"user_id": 301, "age_bucket": "21-25"},
{"user_id": 302, "age_bucket": "26-30"},
{"user_id": 303, "age_bucket": "31-35"},
{"user_id": 304, "age_bucket": "21-25"}
]
}
โบ
Output:
[
{"age_bucket": "21-25", "activity_type": "send", "percentage": 38.8},
{"age_bucket": "26-30", "activity_type": "open", "percentage": 14.6},
{"age_bucket": "31-35", "activity_type": "open", "percentage": 16.2},
{"age_bucket": "31-35", "activity_type": "send", "percentage": 30.4}
]
๐ก Note:
Total time = 50.0. All three age groups are represented with different activity patterns. Age group 21-25 focuses on sending (38.8%), 26-30 on opening (14.6%), and 31-35 has both activities (30.4% send, 16.2% open).
Visualization
Tap to expand
Understanding the Visualization
1
Data Collection
Gather user activities (like tracking customer movements) and demographic information
2
Join & Group
Combine activity data with age groups, then aggregate time spent by each age-activity combination
3
Calculate Grand Total
Use window functions to calculate total time across all users and activities
4
Compute Percentages
Divide each group's time by the grand total and multiply by 100, rounding to 2 decimal places
Key Takeaway
๐ฏ Key Insight: Window functions allow us to calculate both subtotals and grand totals in a single query, making percentage calculations efficient and elegant without requiring multiple database scans or complex subqueries.
Time & Space Complexity
Time Complexity
O(n log n)
Single scan with grouping operation
โก Linearithmic
Space Complexity
O(k)
Where k is number of unique age_bucket and activity_type combinations
โ Linear Space
Constraints
- 1 โค number of activities โค 104
- 1 โค number of age records โค 103
- 0.0 โค time_spent โค 100.0
- activity_type is either 'send' or 'open'
- age_bucket is one of '21-25', '26-30', '31-35'
- All user_ids in Activities table exist in Age table
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code