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
Coding & Algorithms
Software Engineer
Roku
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
Sorting
Single pass aggregation
Hash maps
Event data cleaning
Sessionization
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported

Related Questions