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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

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
Strings
Two Pointers
Parsing
Encoding
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • compress takes lowercase letters only and emits <count><char> per run.
  • decompress takes a valid encoding where counts can be any positive integer.
  • decompress(compress(s)) == s must 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 *...


Submit Your Answer
Markdown supported