Expand Nested Repeat Templates in Promo Message Strings
Last updated: November 29, 2026
Quick Overview
Promo message templates use the syntax k[text] to repeat text k times, and repeats can nest, for example `2[ride3[!]]`. Given a valid template, return the fully expanded string.
Uber
November 29, 20260
0
0 solved
Promo message templates use the syntax k[text] to repeat text k times, and repeats can nest, for example `2[ride3[!]]`. Given a valid template, return the fully expanded string.
A string parsing question in Uber technical screens. It tests stack-based parsing of nested structures and correct handling of multi-digit counts.
What the Interviewer Expects
- Uses a stack of (prefix, count) frames or recursion
- Parses multi-digit repeat counts
- Builds strings with lists to avoid quadratic concatenation
- Handles letters outside any brackets
- States complexity in terms of output size
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
- How would you cap the output size to prevent a template from exploding memory?
- Return only the character at position i of the expansion without building it.
- Support an escape so literal brackets can appear in the message.
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 string of letters, digits and brackets; every
[is preceded by a positive integer. - Output: expanded string.
- Counts may be more than one digit....
High-Level Design
Scan left to right with a stack. Keep the string built so far at the current level and the number being read. On a digit, extend the number. On [, push (current_text, number) and start fresh. On `...