Expand Repeated Blocks Like 3[ab] in a Notification Template

Last updated: November 29, 2026

Quick Overview

Given a compact notification template where k[text] means text repeated k times and blocks may nest, such as 2[a3[b]c], return the fully expanded string.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a compact notification template where k[text] means text repeated k times and blocks may nest, such as 2[a3[b]c], return the fully expanded string.

Meta interviews frequently include a stack-based string parsing problem, and nested repetition is a favorite because it reveals whether a candidate can manage multi-digit numbers and nesting without recursion bugs.

What the Interviewer Expects
  • Parse multi-digit repeat counts
  • Use a stack of (prefix, count) frames for nesting
  • Build strings with lists to avoid quadratic concatenation
  • Handle plain text with no brackets
  • Offer a recursive descent version and compare
Key Topics to Cover
stack
string parsing
nested structures
recursion
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 guard against a template that expands to gigabytes?
  • How would you return only the character at index i without expanding?
  • How would you validate malformed input like unmatched brackets?
  • How would you support escaping a literal bracket?
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: digits, lowercase letters, [ and ]. Input is well formed; every [ is preceded by a positive integer.
  • Output: the expanded string.
  • Counts can be multi-digit, like 12[x]....
High-Level Design

Scan left to right with a stack. Keep cur (the text being built at the current depth) and num (the digits read so far).

  • Digit: num = num * 10 + digit.
  • [: push (cur, num), then start a f...

Submit Your Answer
Markdown supported