PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates understanding of string parsing and nested structure handling, including skills in recursion, stack-based parsing, tokenization, and managing repetition semantics.

  • medium
  • Waymo
  • Coding & Algorithms
  • Software Engineer

Expand Nested Repetition Expressions

Company: Waymo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement a function that expands a valid encoded string containing nested parenthesized repetition expressions. Rules: - Ordinary characters are appended to the output as-is. - A group starts with `(` and ends with `)`. - Every closing parenthesis is immediately followed by a repetition count in the form `{k}`, where `k` is a positive integer. - The decoded contents of the group should be repeated exactly `k` times. - Groups may be nested. Examples: - Input: `"abs(cs){3}g"` - Output: `"abscscscsg"` - Input: `"a(b(c){2}){2}"` - Output: `"abccbcc"` Return the fully expanded string. You may solve this using either recursion or an explicit stack.

Quick Answer: This question evaluates understanding of string parsing and nested structure handling, including skills in recursion, stack-based parsing, tokenization, and managing repetition semantics.

Implement a function that expands a valid encoded string containing nested parenthesized repetition expressions. Rules: - Ordinary characters are appended to the output as-is. - A group starts with `(` and ends with `)`. - Every closing parenthesis is immediately followed by a repetition count in the form `{k}`, where `k` is a positive integer. - The decoded contents of the group should be repeated exactly `k` times. - Groups may be nested. Examples: - `"abs(cs){3}g"` expands to `"abscscscsg"` - `"a(b(c){2}){2}"` expands to `"abccbcc"` Return the fully expanded string. You may solve this using either recursion or an explicit stack.

Constraints

  • 0 <= len(s) <= 10^5
  • The input string is always valid according to the given grammar
  • Each repetition count `k` is a positive integer
  • The total length of the expanded output will not exceed 10^6 characters

Examples

Input: "abs(cs){3}g"

Expected Output: "abscscscsg"

Explanation: The group `cs` is repeated 3 times, giving `abs` + `cscscs` + `g`.

Input: "a(b(c){2}){2}"

Expected Output: "abccbcc"

Explanation: Inner group `(c){2}` becomes `cc`, so outer group becomes `bcc`, repeated twice after the initial `a`.

Input: ""

Expected Output: ""

Explanation: An empty string expands to an empty string.

Input: "plain"

Expected Output: "plain"

Explanation: There are no repetition groups, so the string stays unchanged.

Input: "(ab){10}"

Expected Output: "abababababababababab"

Explanation: The group `ab` is repeated 10 times. This checks correct handling of multi-digit counts.

Input: "xy((zz){1}yw){3}q"

Expected Output: "xyzzywzzywzzywq"

Explanation: Inner `(zz){1}` becomes `zz`, so the outer group becomes `zzyw`, repeated 3 times, with `xy` before it and `q` after it.

Input: "(a(b){2}c){2}d"

Expected Output: "abbcabbcd"

Explanation: Inside the outer group, `(b){2}` becomes `bb`, so the group is `abbc`, repeated twice, then followed by `d`.

Hints

  1. When you see `(`, solve the substring inside it first before applying the repetition count that follows `)`.
  2. Track your current position in the string so nested groups can be parsed without rescanning characters.
Last updated: May 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Paths to Target Nodes - Waymo (medium)
  • Implement Safe Average Function - Waymo (medium)
  • Serialize Expression Tree Minimizing Parentheses - Waymo (medium)
  • Find Shortest Knight Path - Waymo (medium)
  • Implement Fibonacci generator, balanced BST, frozenset - Waymo (medium)