All questions
Easy2026-07-30

Run-Length Encoding with Consecutive Count

Company
Oracle
Role

Senior SDE

Round

Onsite (Stage 3)

StringsTwo PointersEncoding

Problem Statement

To reduce the size of messages transmitted over the internet, a compression algorithm encodes consecutive repeating characters in a string.

Implement this compression:

  • Scan the string left to right and group consecutive identical characters
  • If a character appears once, add just the character to the output
  • If a character appears more than once consecutively, add the character followed by the count of consecutive occurrences

Constraints

  • 1 <= s.length <= 10^5
  • s consists of lowercase English letters only

Example

Input: "aaaaabbbccca"

Output: "a5b3c3a"

Explanation:

  • aaaaaa5 (5 consecutive a's)
  • bbbb3 (3 consecutive b's)
  • cccc3 (3 consecutive c's)
  • aa (single a, no number)

Input: "abcdef"

Output: "abcdef" (no consecutive repeats)

Input: "aabbaabb"

Output: "a2b2a2b2"

Follow-ups

  1. What if the count itself is multi-digit (e.g., 12 consecutive chars)? Does your encoding still decode uniquely?
  2. Can the compressed string ever be longer than the input? When?
  3. How would you implement the decompression function?
  4. What's the worst-case compression ratio?
🧠

No solution provided

Think through it. That's how you build real interview muscle.

Share: