Create a Session Bar Chart - Problem
Create a Session Bar Chart

You're building an analytics dashboard for a mobile application and need to create a session duration bar chart to visualize user engagement patterns.

Given a Sessions table where each row represents a user session with its duration in seconds, you need to categorize these sessions into predefined time bins and count how many sessions fall into each category.

Table: Sessions
Column NameType
session_idint
durationint

The time bins are:
[0-5) - Less than 5 minutes (0-299 seconds)
[5-10) - 5 to 10 minutes (300-599 seconds)
[10-15) - 10 to 15 minutes (600-899 seconds)
15 minutes or more - 15+ minutes (900+ seconds)

Goal: Return a result table with two columns: bin (the time range) and total (count of sessions in that range). All bins must be included even if they have 0 sessions.

Input & Output

example_1.sql — Basic Sessions
$ Input: Sessions table: | session_id | duration | |------------|----------| | 1 | 30 | | 2 | 199 | | 3 | 299 | | 4 | 580 | | 5 | 900 |
Output: | bin | total | |-------------------|-------| | [0-5> | 3 | | [5-10> | 1 | | [10-15> | 0 | | 15 minutes or more| 1 |
💡 Note: Sessions 1,2,3 (30s, 199s, 299s) are all under 5 minutes. Session 4 (580s) is between 5-10 minutes. No sessions in 10-15 minutes. Session 5 (900s = 15 minutes exactly) goes into the 15+ category.
example_2.sql — Edge Cases
$ Input: Sessions table: | session_id | duration | |------------|----------| | 1 | 0 | | 2 | 300 | | 3 | 600 | | 4 | 899 | | 5 | 3600 |
Output: | bin | total | |-------------------|-------| | [0-5> | 1 | | [5-10> | 1 | | [10-15> | 1 | | 15 minutes or more| 2 |
💡 Note: Testing boundary conditions: 0s goes to [0-5>, 300s (exactly 5 min) goes to [5-10>, 600s (exactly 10 min) goes to [10-15>, 899s (14:59) stays in [10-15>, and 3600s (1 hour) goes to 15+ minutes.
example_3.sql — Empty Bins
$ Input: Sessions table: | session_id | duration | |------------|----------| | 1 | 50 | | 2 | 100 | | 3 | 2000 |
Output: | bin | total | |-------------------|-------| | [0-5> | 2 | | [5-10> | 0 | | [10-15> | 0 | | 15 minutes or more| 1 |
💡 Note: All sessions fall into either the shortest ([0-5>) or longest (15+) bins, leaving the middle bins empty. This shows that all bins must appear in results even with 0 count.

Visualization

Tap to expand
Session Duration Analytics DashboardRaw Session DataSession DataID:1 | 150s (2.5m)ID:2 | 450s (7.5m)ID:3 | 720s (12m)ID:4 | 1200s (20m)ID:5 | 90s (1.5m)ID:6 | 840s (14m)ID:7 | 2100s (35m)CASE WHEN Processor[0-5) minutes0 ≤ duration < 300[5-10) minutes300 ≤ duration < 600[10-15) minutes600 ≤ duration < 90015+ minutesduration ≥ 900SUMCounts each binProcessBar Chart Results2[0-5): 2 sessions(150s, 90s)1[5-10): 1 session(450s)2[10-15): 2 sessions(720s, 840s)215+: 2 sessions(1200s, 2100s)Results🚀 Single Pass EfficiencyOne scan processes all 7 sessions into 4 bins simultaneouslyTime Complexity: O(n) | Space Complexity: O(1)
Understanding the Visualization
1
Scan Sessions
Read each session duration from the table
2
Apply Rules
Use CASE WHEN to determine which time bin each session belongs to
3
Count Categories
SUM() function counts how many sessions fall into each bin
4
Format Output
Present results as bin name and total count pairs
Key Takeaway
🎯 Key Insight: CASE WHEN with conditional aggregation allows us to categorize and count data in multiple bins with a single table scan, making it much more efficient than separate queries for each bin.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through the Sessions table to categorize and count all sessions

n
2n
Linear Growth
Space Complexity
O(1)

Only stores the aggregated counts, no additional data structures needed

n
2n
Linear Space

Constraints

  • 1 ≤ session_id ≤ 105
  • 0 ≤ duration ≤ 106 (duration in seconds)
  • All bins must be present in the result, even if they have 0 sessions
  • Results should be ordered by bin sequence: [0-5>, [5-10>, [10-15>, 15 minutes or more
Asked in
Meta 45 Google 38 Amazon 32 Netflix 28
52.4K Views
High Frequency
~15 min Avg. Time
1.8K 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