Expand a Compressed Nested Repetition String Like 3[a2[bc]]
Last updated: November 29, 2026
Quick Overview
Given an encoded string where k[s] means s repeated k times, with arbitrary nesting and multi-digit k, return the fully expanded string.
0
0
0 solved
Given an encoded string where k[s] means s repeated k times, with arbitrary nesting and multi-digit k, return the fully expanded string.
A Google technical screen staple that tests parsing discipline. Interviewers look for a stack-based or recursive-descent parser that handles nesting, multi-digit counts, and plain letters between groups, plus awareness that output size can explode.
What the Interviewer Expects
- Chooses a stack (or recursion) to handle nesting
- Accumulates multi-digit counts
- Pushes the partial string and count on '[' and combines on ']'
- Builds strings with lists to avoid quadratic concatenation
- Discusses the output size blowup and lazy alternatives
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
- Return only the k-th character of the decoded string without building it.
- Validate malformed input and report the error position.
- Encode: given a string, produce the shortest encoding in this format.
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: well-formed encoding with lowercase letters, digits, and brackets.
- Digits only appear right before
[; k is a positive integer, possibly multi-digit. - Output: expanded string....
High-Level Design
Scan left to right with a stack of (prefix_parts, count) frames. Keep a cur list of characters being built at the current depth and a running num. Digit: num = num * 10 + d. [: push `(cur, n...