Expand Run-Length Encoded Text With Multi-Digit Counts

Last updated: November 29, 2026

Quick Overview

Given a compressed string such as "a3b12c" where each character is followed by an optional repeat count (default 1, possibly several digits), return the expanded string. A backslash escapes a digit so it can be repeated as a literal character.

Zoho
Coding & Algorithms
Software Engineer
Zoho
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given a compressed string such as "a3b12c" where each character is followed by an optional repeat count (default 1, possibly several digits), return the expanded string. A backslash escapes a digit so it can be repeated as a literal character.

Zoho's early programming rounds favour string manipulation problems solved from scratch without library shortcuts, the kind of parsing that sits under features like compact storage of repeated characters in Zoho Writer exports. The interviewer checks careful index handling, multi-digit parsing and how you treat malformed input.

What the Interviewer Expects
  • Parse counts with more than one digit correctly
  • Default the count to 1 when no digits follow
  • Handle the escape character so digits can be literals
  • Reject or report malformed input such as a leading digit
  • Build the result in a list and join once instead of repeated concatenation
Key Topics to Cover
String parsing
Two pointers
Run-length encoding
Input validation
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
  • How would you write the inverse function that compresses a string?
  • What if the expanded output could be gigabytes; how would you stream it?
  • How would you support counts of zero?
  • How do you guard against a malicious count like a999999999?
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

Input is a string of tokens. A token is one character (or a backslash plus one character) followed by zero or more digits. Output is the expansion. A digit that appears where a character is expected i...

High-Level Design

Walk the string once with index i. Read the character (unescaping if it is a backslash), then advance j over the digits that follow. The slice s[i:j] is the count. Append ch * count to a list ...


Submit Your Answer
Markdown supported

Related Questions