Compute Per-Channel Watch Time From Playback Heartbeats
Last updated: November 29, 2026
Quick Overview
Given a device's playback heartbeats as (timestamp_seconds, channel_id) pairs, return total watch seconds per channel, counting only gaps between consecutive heartbeats on the same channel that are at most max_gap seconds.
Roku
November 29, 20260
0
0 solved
Given a device's playback heartbeats as (timestamp_seconds, channel_id) pairs, return total watch seconds per channel, counting only gaps between consecutive heartbeats on the same channel that are at most max_gap seconds.
A warm-up in the style of a Roku phone screen: viewing metrics for The Roku Channel and partner channels are derived from periodic heartbeats that devices send while video plays. The interviewer checks that you clarify how gaps, channel switches and out-of-order events are treated before writing a clean single pass.
What the Interviewer Expects
- Asks how a channel switch, a long pause and duplicate timestamps should count
- Sorts events instead of assuming arrival order
- Credits a gap only when both ends are on the same channel and the gap is within max_gap
- Linear pass after sorting with a dictionary of totals
- States O(n log n) time and O(c) extra space for c channels
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you attribute a partial gap when the device sends a 'stopped' event in the middle?
- How would you compute this for millions of devices in a streaming job with late events?
- What changes if heartbeats include a playback position and you want content seconds instead of wall seconds?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a list of (ts, channel) heartbeats for one device, possibly unsorted. Devices beat about every 30 seconds while playing. Output is {channel: seconds}. Rules I would confirm:
- A gap count...
High-Level Design
Sort by timestamp, walk adjacent pairs, and add ts - prev_ts to the channel when the pair qualifies. Keep only the previous event and a dictionary of totals, so this is the same shape as a per-devic...