Run-Length Compress a Reaction Event String In Place
Last updated: November 29, 2026
Quick Overview
Given a list of characters representing a burst of reaction events (for example l for like, h for heart), compress it in place so each run becomes the character followed by its count if the count exceeds one, and return the new length.
Meta
November 29, 20260
0
0 solved
Given a list of characters representing a burst of reaction events (for example l for like, h for heart), compress it in place so each run becomes the character followed by its count if the count exceeds one, and return the new length.
Live video on Facebook produces bursts of identical reactions, and compact encodings save bandwidth. Interviewers use this to check in-place two-pointer writing, multi-digit counts, and the guarantee that the write pointer never overtakes the read pointer.
What the Interviewer Expects
- Use separate read and write pointers
- Write the count only when the run length exceeds one
- Write multi-digit counts digit by digit
- Return the new length and leave the tail untouched
- Argue why writing never overwrites unread input
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
- Why can the write pointer never pass the read pointer?
- How would you decompress, and what if output could be huge?
- When does run-length encoding make data larger?
- How would you stream this over a socket?
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: non-empty list of characters (letters only).
- Modify the list so its first
Lentries are the compressed form; returnL. - Runs of length 1 are written as just the character; longer runs ...
High-Level Design
Walk with read. At each run start, advance read to the end of the run and compute its length. Write the character at write, then, if the length exceeds one, write each digit of str(length).
T...