Merge Overlapping Prime Video Playbacks Into Cross-Device Viewing Sessions
Last updated: November 29, 2026
Quick Overview
Given playback intervals per user from multiple devices that can overlap, compute each user's distinct minutes watched and number of viewing sessions, where a new session starts after more than five idle minutes.
Amazon
November 29, 20260
0
0 solved
Given playback intervals per user from multiple devices that can overlap, compute each user's distinct minutes watched and number of viewing sessions, where a new session starts after more than five idle minutes.
A Python data manipulation question in the style of Prime Video engagement pipelines, where the same account streams on a TV and a phone at once and naive sums double count. The interviewer checks interval merging, correct handling of gaps versus overlaps, and clean grouping by user.
What the Interviewer Expects
- Group intervals by user and sort by start time
- Merge overlapping intervals so overlapping minutes count once
- Separate the overlap rule from the five-minute session gap rule
- Drop or flag zero and negative length intervals
- Explain O(n log n) cost from sorting
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you report minutes per device without double counting the total?
- How would you do this in SQL with window functions?
- How would you handle a playback that is still in progress with no end time?
- How would you scale this to billions of events per day?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Input: a list of (user_id, start_min, end_min) tuples, one per playback segment, in minutes since some epoch, from any device. Questions to settle: are intervals half-open (assume yes, so [0, 30) ...
Approach
Two separate concerns:
- Watched minutes: overlapping segments, for example a TV and a phone both playing, must be merged so minutes are counted once. This is the standard merge intervals pass a...