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 NameType
activity_idint
user_idint
activity_typeenum ('send', 'open')
time_spentdecimal

Age Table

Column NameType
user_idint
age_bucketenum ('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
Raw DataActivities + Age InfoMultiple tablesuser_id as join keyJOIN & GROUPCombine & AggregateGROUP BY age_bucket,activity_typeWindow FunctionSUM() OVER()Calculate grand totalacross all groupsFinal Percentages21-25 send: 35.71%21-25 open: 21.43%26-30 send: 28.57%Step 1Step 2Step 3Step 4
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

n
2n
โšก Linearithmic
Space Complexity
O(k)

Where k is number of unique age_bucket and activity_type combinations

n
2n
โœ“ 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
Asked in
Meta 45 Snapchat 38 TikTok 32 Instagram 28
42.5K Views
High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen