The Ad-Free Sessions interview question involves comparing two timelines. You have a table of user Playback sessions (with start and end times) and a table of Ads (with a timestamp of when they were shown). You need to find all session IDs where no advertisement was shown during the session.
Amazon uses this Ad-Free Sessions coding problem to test your ability to perform non-equi joins. Most joins are based on equality (A.id = B.id), but this problem requires joining or filtering based on an inequality or range (timestamp BETWEEN start_time AND end_time).
This utilizes the Anti-Join interview pattern. You want to find all sessions that do not have a corresponding entry in the Ads table that falls within their time range. This is usually implemented using a LEFT JOIN where the right side is NULL, or a NOT EXISTS clause.
For Session 1, Ad A falls between the start and end. For Session 2, no ads fall in the range. Session 2 is "Ad-Free."
timestamp >= start_time AND timestamp <= end_time inside a NOT EXISTS subquery is usually sufficient.NOT IN, being unaware that if the subquery returns any NULL values, the entire expression may return false.When asked to find records that don't match a condition, always prefer NOT EXISTS over NOT IN. It is safer regarding null values and often optimized better by the database engine.