Decode Nested Repetition Strings with Stacks

Quick Overview

Decode nested repetition syntax with saved prefix and count frames. Handle multi-digit counts, prove the stack invariant, and account honestly for expanded output and repeated string copying.

Decode Nested Repetition Strings with Stacks

Company: Compass

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Decode strings using the pattern k[encoded], where the bracketed content is repeated k times and may contain nested repetitions. Plain letters remain in order. ### Constraints and Clarifications The source identifies problem394. The [official Decode String reference](https://leetcode.com/problems/decode-string/description/) supplies this practice contract: valid syntax, lowercase letters, positive repeat counts from1 to300, encoded length1–30, and decoded length at most100000. Digits appear only in repeat counts, not as literal output characters. These are reference-context constraints, not additional recalled interview requirements. ### Example "2[a3[b]]c" decodes to "abbbabbbc". ### What a Strong Answer Covers Track multi-digit counts and nested contexts with stacks or equivalent frames. Explain what is saved at an opening bracket, what is restored at a closing bracket, and the cost of materializing expanded output. ### Follow-up Questions - Why does a single repeat counter fail for nested brackets? - How can repeated string concatenation affect complexity?

Overview: Decode nested repetition syntax with saved prefix and count frames. Handle multi-digit counts, prove the stack invariant, and account honestly for expanded output and repeated string copying.

Compass logo
Compass
Oct 25, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Decode strings using the pattern k[encoded], where the bracketed content is repeated k times and may contain nested repetitions. Plain letters remain in order.

Constraints and Clarifications

The source identifies problem394. The official Decode String reference supplies this practice contract: valid syntax, lowercase letters, positive repeat counts from1 to300, encoded length1–30, and decoded length at most100000. Digits appear only in repeat counts, not as literal output characters. These are reference-context constraints, not additional recalled interview requirements.

Example

"2[a3[b]]c" decodes to "abbbabbbc".

What a Strong Answer Covers Guidance

Track multi-digit counts and nested contexts with stacks or equivalent frames. Explain what is saved at an opening bracket, what is restored at a closing bracket, and the cost of materializing expanded output.

Follow-up Questions Guidance

  • Why does a single repeat counter fail for nested brackets?
  • How can repeated string concatenation affect complexity?