Ad-Free Sessions - Problem

You are given two tables: Playback and Ads.

The Playback table contains information about customer viewing sessions with start and end times. Each session has a unique session_id and belongs to a customer_id.

The Ads table contains information about ads shown to customers, with each ad having a unique ad_id, the customer_id who viewed it, and the timestamp when it was shown.

Task: Find all sessions that did not have any ads shown during them. A session runs during the inclusive interval between start_time and end_time. An ad is considered shown during a session if its timestamp falls within this interval.

Table Schema

Playback
Column Name Type Description
session_id PK int Unique identifier for each session
customer_id int ID of the customer watching this session
start_time int Start time of the session
end_time int End time of the session
Primary Key: session_id
Ads
Column Name Type Description
ad_id PK int Unique identifier for each ad
customer_id int ID of the customer who viewed this ad
timestamp int Time when the ad was shown
Primary Key: ad_id

Input & Output

Example 1 — Basic Ad-Free Session
Input Tables:
Playbook
session_id customer_id start_time end_time
1 1 1 3
2 1 4 5
3 2 4 5
Ads
ad_id customer_id timestamp
1 1 5
2 2 6
Output:
session_id
1
3
💡 Note:

Session 1 (customer 1, time 1-3): No ads shown during this period (ad was at time 5). Session 2 (customer 1, time 4-5): Has ad at time 5, so excluded. Session 3 (customer 2, time 4-5): No ads during this period (ad was at time 6).

Example 2 — All Sessions Have Ads
Input Tables:
Playbook
session_id customer_id start_time end_time
1 1 1 5
2 2 2 4
Ads
ad_id customer_id timestamp
1 1 3
2 2 4
Output:
session_id
💡 Note:

No ad-free sessions exist. Session 1 has an ad at time 3 (within range 1-5), and session 2 has an ad at time 4 (within range 2-4).

Constraints

  • 1 ≤ session_id, ad_id ≤ 100
  • 1 ≤ customer_id ≤ 100
  • 1 ≤ start_time ≤ end_time ≤ 300
  • 1 ≤ timestamp ≤ 300

Visualization

Tap to expand
Ad-Free Sessions INPUT DATA Sessions: ID Start End User S1 10:00 10:30 U1 S2 11:00 11:45 U2 S3 12:00 12:20 U3 Ads Shown: Ad ID Timestamp A1 10:15 A2 11:20 Timeline View: 10:00 11:00 12:00 S1 S2 S3 A1 A2 ALGORITHM STEPS 1 Load Sessions Get all session records with time ranges 2 Load Ad Events Get all ad timestamps from ads table 3 LEFT JOIN + Filter Join ads where timestamp BETWEEN start AND end Sessions Ads 4 Find NULL Matches WHERE ad_id IS NULL means no ads shown SELECT session_id FROM sessions s LEFT JOIN ads WHERE NULL FINAL RESULT Analysis Results: Session S1 (10:00-10:30) Ad A1 at 10:15 - HAS ADS Session S2 (11:00-11:45) Ad A2 at 11:20 - HAS ADS Session S3 (12:00-12:20) No ads found - AD-FREE! OK Output: Ad-Free Sessions [ S3 ] 1 session with no ads Key Insight: Use LEFT JOIN to match sessions with ads where ad timestamp falls within session duration. Sessions where the ad_id IS NULL after the join are the ad-free sessions - they had no matching ads during their entire viewing period. This is the optimal O(n log n) approach. TutorialsPoint - Ad-Free Sessions | Optimal Solution (LEFT JOIN with NULL check)
Asked in
Netflix 28 YouTube 19 Spotify 15
25.4K Views
Medium Frequency
~12 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