Iterator Over a Run-Length Encoded Shipment Scan Log With Bulk Skips
Last updated: November 29, 2026
Quick Overview
A compressed scan log stores pairs (count, package_type). Implement next(n), which consumes n scans and returns the type of the last scan consumed, or -1 if fewer than n scans remain.
Amazon
November 29, 20260
0
0 solved
A compressed scan log stores pairs (count, package_type). Implement next(n), which consumes n scans and returns the type of the last scan consumed, or -1 if fewer than n scans remain.
Sort centers compress repetitive scan logs, and downstream jobs need to skip ahead without decompressing. The interviewer checks that you walk runs with a pointer and a remaining-count, achieving cost proportional to runs touched, not scans skipped.
What the Interviewer Expects
- Never expand the encoding into individual scans
- Track the current run index and how many scans remain in it
- Consume whole runs while n exceeds the remainder
- Return -1 and exhaust the iterator when not enough scans remain
- Skip zero-count runs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support random access to the i-th scan with binary search over prefix counts?
- How would you append new runs while iterating?
- What changes if counts can exceed 64-bit integers in a lower-level language?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input at construction: a flat list [c0, t0, c1, t1, ...]. Each next(n) with n >= 1 consumes n scans. Output: the type of the n-th consumed scan, or -1 if the log runs out (and the log is then empt...
High-Level Design
Keep i, the index of the current run, and left, the number of unconsumed scans in it. On next(n): while a run exists and n > left, subtract left from n and move to the next run. If no run re...