Medium · System Design

RLE Iterator

Given a run-length encoded array encoding, where encoding[2i] says how many times encoding[2i+1] repeats, implement next(n): consume the next n elements of the decoded sequence and return the last one consumed, or -1 if fewer than n elements remain (the sequence is then exhausted).

Examples

Example 1

[3,0,2,2,1,1] · next 4,2

Output: [2, 1]

Example 2

[3,8,0,9,2,5] · next 2,1,1

Output: [8, 8, 5]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems