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 times
  • Ads - 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
๐ŸŽฌ StreamingFlix - Ad-Free Session Detection๐Ÿ“บ Viewing SessionsSession 1: 10min-50minSession 2: 20min-60minSession 3: 70min-90min๐Ÿ“ข Ad ImpressionsAd @25minAd @15minAd @65minโœ… Ad-Free ResultsโŒ Session 1 (has ad)โœ… Session 2 (ad-free)โœ… Session 3 (ad-free)LEFT JOIN ON customer + time overlap โ†’ Filter NULL ads
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

No additional space beyond result set, database handles JOIN internally

n
2n
โœ“ 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
Asked in
Netflix 45 Amazon 38 Hulu 28 YouTube 22
23.4K Views
Medium Frequency
~15 min Avg. Time
892 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