Iterate Over a Run-Length Encoded Watch Event Stream

Last updated: November 29, 2026

Quick Overview

Watch events are stored run-length encoded as [count, value, count, value, ...]. Implement next(n), which consumes n events and returns the last one consumed, or -1 if fewer than n remain.

ByteDance
Coding & Algorithms
Software Engineer
ByteDance
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Watch events are stored run-length encoded as [count, value, count, value, ...]. Implement next(n), which consumes n events and returns the last one consumed, or -1 if fewer than n remain.

Douyin's client logging compresses repeated events (for example many identical heartbeat pings) with run-length encoding before upload. This online assessment problem checks that you can consume the encoding lazily without expanding it, and that your iterator state is minimal and correct across calls.

What the Interviewer Expects
  • Never expand the encoding into a full list
  • Track a position and how much of the current run is used
  • Skip runs with zero count
  • Return -1 once exhausted and stay exhausted
  • Handle n spanning multiple runs
Key Topics to Cover
iterators
run-length encoding
streams
lazy evaluation
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
  • Add a peek() that does not consume.
  • How would you support seeking backward by n events?
  • Design the encoder for a live stream of events with bounded memory.
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 encoding has even length, counts are non-negative, and counts can be huge (up to 10^9). next(n) with n >= 1 returns the value of the n-th next element or -1 if the stream runs out, consuming...

High-Level Design

Keep i, the index of the current run's count, and used, how many of that run have been consumed. On next(n), compare n to what is left in the run. If it fits, advance used and return the run's...


Submit Your Answer
Markdown supported