Run-Length Encode and Decode Repeated Characters in Logs
Last updated: November 29, 2026
Quick Overview
Implement compress(s), which run-length encodes a string of lowercase letters as count followed by character ("aaab" becomes "3a1b"), and decompress(t), which reverses it, including counts of two or more digits.
0
0
0 solved
Implement compress(s), which run-length encodes a string of lowercase letters as count followed by character ("aaab" becomes "3a1b"), and decompress(t), which reverses it, including counts of two or more digits.
A classic Google phone screen opener that looks trivial but separates careful coders from rushed ones. The interviewer watches for off-by-one errors at the end of the string, multi-digit counts during decoding, and whether you build strings efficiently instead of with repeated concatenation.
What the Interviewer Expects
- Correctly flushes the final run after the loop
- Parses multi-digit counts in the decoder
- Uses a list and join rather than quadratic string concatenation
- Verifies the round trip on a few inputs
- Discusses when this encoding expands rather than shrinks the data
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
- What changes if the input may contain digits itself?
- How would you make the encoding never larger than the input?
- How would you decode a stream in fixed memory without materializing the whole output?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
compresstakes lowercase letters only and emits<count><char>per run.decompresstakes a valid encoding where counts can be any positive integer.decompress(compress(s)) == smust hold....
High-Level Design
Compression is a single scan with a run start pointer: advance j while s[j] == s[i], emit j - i and the character, then jump i = j. Decompression accumulates digits into a number (`num = num *...