Run-Length Encode Repeated SKUs in a Packing List
Last updated: November 29, 2026
Quick Overview
Given a packing list as a sequence of SKU strings, compress consecutive repeats into (sku, count) pairs, and write the matching decoder. Then handle the variant where the output must be a single string and SKUs themselves may contain digits.
Amazon
November 29, 20260
0
0 solved
Given a packing list as a sequence of SKU strings, compress consecutive repeats into (sku, count) pairs, and write the matching decoder. Then handle the variant where the output must be a single string and SKUs themselves may contain digits.
A typical Amazon phone screen opener about compressing manifests sent to handheld scanners. The interviewer watches for off-by-one errors at the end of runs and for whether you notice that a naive string format is ambiguous when SKUs contain digits.
What the Interviewer Expects
- Group consecutive equal SKUs in one pass
- Flush the final run correctly
- Write a decoder and show round-trip correctness
- Spot the ambiguity in a naive string encoding and fix it with a delimiter or length prefix
- Mention when compression does not help
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 encode so the output is never longer than the input?
- How would you stream-decode without holding the whole list in memory?
- What if non-adjacent repeats should also be grouped?
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 list may be empty. Encoding returns a list of (sku, count) tuples with count >= 1. The string variant must decode unambiguously even if a SKU is like 'B07X9'. Round trip `decode(encode(x)) == ...
High-Level Design
Pairs version: keep the current SKU and a counter; when the next item differs, append the pair and reset; after the loop append the last pair if any.
String version: a format like B07X93 is ambiguo...