RLE Iterator
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
A run-length encoded sequence is given as an even-length array encoding: for every even index i, the value encoding[i + 1] is repeated encoding[i] times. For example [3,8,0,9,2,5] stands for [8,8,8,5,5] (the run of 9 has length zero).
Implement RLEIterator(encoding) and next(n).
next(n) consumes the next n elements of the decoded sequence and returns the **last** element consumed. If fewer than n elements remain, it consumes all of them and returns -1. Once the sequence is exhausted, every further call returns -1. Counts can be huge, so do not expand the sequence.
Examples
Constraints
- 2 <= encoding.length <= 1000, and `encoding.length` is even
- 0 <= encoding[i] <= 10^9
- 1 <= n <= 10^9
- At most 1000 calls to `next`
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.