Decode String
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that breaks a naive Decode String solution is a multi-digit count like 100[a] or a nested group that closes right before another one opens. Google reported this one in July 2026, and it's a classic string and stack problem dressed as a quick warmup. Most people get the examples right and then fail a hidden test on a number like 12 being read as 1 and 2. If you blank on the nesting logic mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution in real time as a safety net.
The problem
Given a valid encoded string s, return its fully decoded form. The encoding rule is k[encoded_string], meaning that the content inside the brackets is repeated exactly k times. Encoded groups may be nested, and adjacent literal or encoded groups are concatenated. For this exercise, assume repetition counts are positive decimal integers and literal characters are lowercase English letters. Function decodeString(s: String) → String Examples Example 1 s = "3[a2[c]]" return = "accaccacc" The inner group 2[c] becomes cc, so the outer group is 3[acc]. Example 2 s = "2[abc]3[cd]ef" return = "abcabccdcdcdef" Decode the two repeated groups independently, then append the literal suffix ef. Constraints 1 <= s.length <= 10^5 s is a valid encoding with balanced brackets. Every repetition count is in [1, 300]. Literal characters are lowercase English letters. The decoded output length is at most 10^5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a stack. Scan left to right. Build the current number digit by digit (num = num * 10 + digit), so 300 parses correctly. Build the current string from letters. On '[', push the current string and the number onto the stack, then reset both. On ']', pop the previous string and the count, and set current = previous + current * count. At the end, current is your answer. The common pitfall is reading only one digit for the count, or forgetting to reset num after pushing. Another is repeated string concatenation in a way that blows up time, but the output cap of 10^5 keeps this safe. A recursive parser works too, with an index pointer. If you freeze on the pop order during the live OA, StealthCoder is the hedge that shows the stack logic while you keep typing.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Decode String cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as decode string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Decode String FAQ
How hard is Decode String really?+
It's medium. The idea is short once you see the stack, but the bugs are in the details: multi-digit counts, resetting state after '[', and the pop order on ']'. Expect to finish in 15 to 20 minutes if you've seen it once.
What's the trick for the Google version?+
Keep two things on a stack at each '[': the string built so far and the repeat count. When you hit ']', pop both, repeat the current segment count times, and append it to the popped string. Parse numbers as num * 10 + digit.
Can I solve it recursively instead of with a stack?+
Yes. Write a helper with a shared index. It reads letters and digits, and on '[' it calls itself to decode the inside, then repeats the result. It returns on ']'. It's the same logic, and the call stack replaces your explicit one.
What edge cases should I test before submitting?+
Test a count like 100[a] for multi-digit parsing, deep nesting like 3[a2[c]], adjacent groups like 2[ab]3[cd], and a trailing literal like ef after a group. Also check a string with no brackets at all.
How do I prepare for this in 48 hours?+
Write the stack solution from scratch twice, then trace 3[a2[c]] by hand. Focus on when you reset num and the current string. Then try the recursive version once. That covers most of the variants of this pattern.