Consume a Run-Length Encoded Sequence through a Stateful Iterator

Quick Overview

Consume a Run-Length Encoded Sequence through a Stateful Iterator. Implement RLEIterator(encoding) and next(n).

Consume a Run-Length Encoded Sequence through a Stateful Iterator

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement `RLEIterator(encoding)` and `next(n)`. The even-length encoding consists of count,value pairs; a zero count contributes no values. Each next call consumes the next n sequence elements and returns the last consumed value. If fewer than n elements remain, consume the remainder and return `-1`. Exhaustion persists across later calls. Preserve the constructor and method protocol; example call sequences illustrate that protocol. ```cpp class RLEIterator { public: RLEIterator(vector<int>& encoding); int next(int n); }; ``` ### Examples **Example 1:** ```text Input: construct RLEIterator([2,4,1,9]); call next(1), next(2), next(1) Output: [4,9,-1] Explanation: The decoded sequence is[4,4,9]; the first two calls consume1 and then2 elements, exhausting it. ``` **Example 2:** ```text Input: construct RLEIterator([0,7,3,5]); call next(4), next(1) Output: [-1,-1] Explanation: The zero run is skipped. Only three5s exist, so the first request exhausts them without a fourth element. ``` ### Constraints - `2 <= encoding.length <= 1000`, and its length is even. - `0 <= encoding[i] <= 10^9` for all encoding entries. - `1 <= n <= 10^9` for each next call. - At most1000 next calls occur. ### Contract considerations The callable is the original stateful class. A transcript is only example notation, not a new public batch function. ```hint Read the boundary condition You need only a run index and each run count still available, not the decoded array. ``` ### Boundary reminders The callable is the original stateful class. A transcript is only example notation, not a new public batch function.

Overview: Consume a Run-Length Encoded Sequence through a Stateful Iterator. Implement RLEIterator(encoding) and next(n).

Google logo
Google
Jun 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement RLEIterator(encoding) and next(n). The even-length encoding consists of count,value pairs; a zero count contributes no values. Each next call consumes the next n sequence elements and returns the last consumed value. If fewer than n elements remain, consume the remainder and return -1. Exhaustion persists across later calls. Preserve the constructor and method protocol; example call sequences illustrate that protocol.

class RLEIterator {
public:
    RLEIterator(vector<int>& encoding);
    int next(int n);
};

Examples

Example 1:

Input: construct RLEIterator([2,4,1,9]); call next(1), next(2), next(1)
Output: [4,9,-1]
Explanation: The decoded sequence is[4,4,9]; the first two calls consume1 and then2 elements, exhausting it.

Example 2:

Input: construct RLEIterator([0,7,3,5]); call next(4), next(1)
Output: [-1,-1]
Explanation: The zero run is skipped. Only three5s exist, so the first request exhausts them without a fourth element.

Constraints

  • 2 <= encoding.length <= 1000 , and its length is even.
  • 0 <= encoding[i] <= 10^9 for all encoding entries.
  • 1 <= n <= 10^9 for each next call.
  • At most1000 next calls occur.

Contract considerations

The callable is the original stateful class. A transcript is only example notation, not a new public batch function.

Boundary reminders

The callable is the original stateful class. A transcript is only example notation, not a new public batch function.