RLE Iterator - Problem
Run-Length Encoding (RLE) Iterator

Imagine you have a compressed sequence of numbers where instead of storing repeated values multiple times, you store pairs of [count, value]. This is called Run-Length Encoding!

For example, the sequence [8,8,8,5,5] can be compressed to [3,8,2,5] meaning "3 times the number 8, then 2 times the number 5".

Your Mission: Design an iterator that can efficiently traverse through this compressed format without expanding the entire sequence into memory.

Key Operations:
โ€ข RLEIterator(encoded) - Initialize with a run-length encoded array
โ€ข next(n) - Skip the next n elements and return the last element consumed. If there aren't enough elements, return -1

Think of it like fast-forwarding through a compressed video - you need to jump ahead efficiently without decompressing everything!

Input & Output

example_1.py โ€” Basic Usage
$ Input: RLEIterator([3,8,0,9,2,5]) next(2), next(1), next(1), next(2)
โ€บ Output: 8, 8, 5, -1
๐Ÿ’ก Note: The sequence is [8,8,8,5,5]. next(2) consumes first 2 elements, returns 8. next(1) consumes 1 more (the last 8), returns 8. next(1) consumes first 5, returns 5. next(2) tries to consume 2 more but only 1 element left, returns -1.
example_2.py โ€” Zero Counts
$ Input: RLEIterator([2,3,0,5,1,7]) next(1), next(2)
โ€บ Output: 3, 7
๐Ÿ’ก Note: Sequence is [3,3,7]. The (0,5) pair contributes no elements. next(1) returns 3, next(2) consumes the remaining 3 and the 7, returning 7.
example_3.py โ€” Large Skip
$ Input: RLEIterator([1000000,1,1,2]) next(999999), next(2)
โ€บ Output: 1, 2
๐Ÿ’ก Note: First pair has 1 million 1's. next(999999) consumes 999,999 ones, returns 1. next(2) consumes the last 1 and the single 2, returns 2.

Constraints

  • 1 โ‰ค encoding.length โ‰ค 1000
  • encoding.length is even
  • 0 โ‰ค encoding[i] โ‰ค 109
  • 1 โ‰ค n โ‰ค 109
  • At most 1000 calls will be made to next

Visualization

Tap to expand
RLE Iterator: Efficient Compressed NavigationTraditional Approach (Memory Hungry):Expand All: [8,8,8,5,5] - Uses lots of memory!Optimal Approach (Smart Pointers):Pair 1(3,8)Pair 2(0,9)Pair 3(2,5)Current PositionBenefits:Memory efficient - no expansion neededFast initialization - O(1) setup timeSmart skipping - jump through compressed segments
Understanding the Visualization
1
Compressed Storage
Store data as [count, value] pairs like video frames with durations
2
Position Tracking
Maintain pointers to current segment and consumption within that segment
3
Efficient Skipping
Jump through segments consuming elements until request is satisfied
Key Takeaway
๐ŸŽฏ Key Insight: Instead of expanding compressed data, maintain smart pointers to navigate efficiently through the original compressed format, preserving both memory and the benefits of compression.
Asked in
Google 24 Amazon 18 Apple 12 Microsoft 8
28.4K Views
Medium Frequency
~15 min Avg. Time
891 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen