Ad-Free Sessions - Problem
You're working for a video streaming platform and need to identify which viewing sessions were completely ad-free.
Given two tables:
Playback- Contains customer viewing sessions with start and end timesAds- Contains ad impressions with timestamps for each customer
Your task is to find all sessions where no ads were shown during the viewing period. An ad is considered shown during a session if its timestamp falls within the inclusive time interval [start_time, end_time].
Goal: Return all session_ids that had zero ads displayed during their runtime.
Input & Output
example_1.sql โ Basic Case
$
Input:
Playback:
| session_id | customer_id | start_time | end_time |
|------------|-------------|------------|---------|
| 1 | 101 | 10 | 50 |
| 2 | 102 | 20 | 60 |
| 3 | 101 | 70 | 90 |
Ads:
| ad_id | customer_id | timestamp |
|-------|-------------|-----------|
| 1 | 101 | 25 |
| 2 | 102 | 15 |
โบ
Output:
| session_id |
|------------|
| 2 |
| 3 |
๐ก Note:
Session 1 has ad 1 at timestamp 25 (within [10,50]). Session 2 has ad 2 at timestamp 15 (outside [20,60]). Session 3 has no ads during [70,90]. So sessions 2 and 3 are ad-free.
example_2.sql โ All Sessions Have Ads
$
Input:
Playbook:
| session_id | customer_id | start_time | end_time |
|------------|-------------|------------|---------|
| 1 | 101 | 0 | 30 |
| 2 | 102 | 10 | 40 |
Ads:
| ad_id | customer_id | timestamp |
|-------|-------------|-----------|
| 1 | 101 | 15 |
| 2 | 102 | 25 |
โบ
Output:
| session_id |
|------------|
๐ก Note:
Session 1 has ad 1 at timestamp 15 (within [0,30]). Session 2 has ad 2 at timestamp 25 (within [10,40]). No sessions are ad-free.
example_3.sql โ No Ads Table
$
Input:
Playback:
| session_id | customer_id | start_time | end_time |
|------------|-------------|------------|---------|
| 1 | 101 | 10 | 50 |
| 2 | 102 | 20 | 60 |
Ads:
| ad_id | customer_id | timestamp |
|-------|-------------|-----------|
โบ
Output:
| session_id |
|------------|
| 1 |
| 2 |
๐ก Note:
No ads exist in the database, so all sessions are ad-free by default.
Visualization
Tap to expand
Understanding the Visualization
1
Collect Sessions
Gather all customer viewing sessions with their time intervals
2
Gather Ad Data
Collect all ad impressions with timestamps and customer IDs
3
Match Overlaps
For each session, find ads that played during the viewing window
4
Filter Ad-Free
Return only sessions that had zero matching ad impressions
Key Takeaway
๐ฏ Key Insight: Use LEFT JOIN to preserve all sessions, then filter WHERE ad_id IS NULL to find sessions with no overlapping ads - this gives us the ad-free viewing experiences!
Time & Space Complexity
Time Complexity
O(n + m)
Single pass through both tables with efficient JOIN operation
โ Linear Growth
Space Complexity
O(1)
No additional space beyond result set, database handles JOIN internally
โ Linear Space
Constraints
- 1 โค session_id, ad_id โค 105
- 1 โค customer_id โค 104
- 0 โค start_time โค end_time โค 106
- 0 โค timestamp โค 106
- Two sessions for the same customer do not intersect
- All session_ids and ad_ids are unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code