Decode Repeated Groups
Updated
Problem statement
You are given an encoded string s. Return the decoded string.
Plain characters are copied as they are. A group looks like (text){k}. It means the decoded text inside the parentheses is repeated k times. Groups can sit inside other groups.
For example, take a(b(c){2}){2}. The inner group (c){2} gives cc, so the outer text is bcc. Repeated twice that is bccbcc, and with the a in front the answer is abccbcc.
Function
decode(s: String) → StringExamples
Example 1
s = "abs(cs){3}g"return = "abscscscsg"The group (cs){3} repeats cs three times, which gives cscscs. With abs in front and g behind, the answer is abscscscsg.
Example 2
s = "a(b(c){2}){2}"return = "abccbcc"The inner group (c){2} gives cc, so the outer text is bcc. Repeating bcc twice gives bccbcc. With a in front, the answer is abccbcc.
Example 3
s = "x(yz){0}q"return = "xq"The group (yz){0} is repeated zero times, so it adds nothing. Only x and q are left, so the answer is xq.
Constraints
- Repeat counts are non-negative integers written inside braces after a closing parenthesis.
- Parentheses and braces are balanced in valid inputs.
More Waymo Phone Screen questions
- 2D Convolution Layer Forward PassPHONE SCREEN · Seen Dec 2026
- Multi-Head Attention Forward PassPHONE SCREEN · Seen Dec 2026
- Numerically Stable SoftmaxPHONE SCREEN · Seen Dec 2026
- Shortest Travel Time in a Grid with Traffic LightsPHONE SCREEN · Seen Nov 2026
- Fixed-Order Task Run Time With CooldownPHONE SCREEN · Seen Oct 2026
- Reorderable Task Scheduler With CooldownPHONE SCREEN · Seen Oct 2026
- Island Area and Water BoundaryPHONE SCREEN · Seen Sep 2026
- Vehicle Maintenance Event ReadinessPHONE SCREEN · Seen Sep 2026
- Obstacle-Aware Return to OriginPHONE SCREEN · Seen Sep 2026
- Shortest Paths to Multiple TargetsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Decode an Encoded StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Parse Indented YAML MappingsUber · PHONE SCREENUber interview questions
- Deserialize XML Into a TreeAdobe · PHONE SCREENAdobe interview questions
- Convert Email Text to Structured MarkupZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
- Parse Variable, Expression, and Application TreesSambaNova Systems · PHONE SCREEN · ONSITE INTERVIEWSambaNova Systems interview questions