Reported September 2026
Amazonstack

Decode an Encoded String

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt at Amazon's Decode an Encoded String, reported in September 2026, is treating the digits as single characters. Then "12[a]" decodes wrong and nested cases fall apart. It's a string problem with a stack at its core, and it looks easy until the nesting shows up. If you've got the OA in a day or two, the goal is to know the shape of the solution before you open the editor. And if you blank mid-assessment, StealthCoder runs invisibly as a safety net and gives you a working decode while the clock keeps moving.

The problem

An encoded string uses positive repeat counts followed by bracketed segments. Decode it using these rules:
k[segment] means the decoded segment is repeated exactly k times.
Segments may be nested.
Letters outside brackets appear once and remain in order.
Return the fully decoded string.

Function
decodeString(s: String) → String

Examples
Example 1
s = "3[a2[c]]"
return = "accaccacc"
The inner block becomes acc, then the outer count repeats it three times.
Example 2
s = "2[ab]3[c]"
return = "ababccc"
The two adjacent encoded blocks decode independently and are concatenated.

Constraints
1 <= s.length <= 10^4
s is a well-formed encoding made of lowercase English letters, digits, and brackets.
Every repeat count is between 1 and 300.
The decoded string has at most 10^5 characters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a stack that saves state at every open bracket. Keep a current string and a current number. On a digit, build the number with num = num * 10 + digit, so multi-digit counts like 12 or 300 work. On "[", push the current string and the number, then reset both. On "]", pop the previous string and count, and set current = previous + current * count. On a letter, append it. The common pitfall is forgetting multi-digit counts, or resetting the number at the wrong time. Another is building the result with repeated string concatenation in a loop, which gets slow, so use a list and join. Output is capped at 10^5 characters, so the work stays linear in output size. A recursive parser with an index pointer also works. If you freeze during the live OA, StealthCoder is the hedge that reads the problem and hands you this stack approach.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Decode an Encoded 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as decode string. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Decode an Encoded String FAQ

How hard is Decode an Encoded String really?+

Medium. The idea is short once you see the stack, but the nesting and multi-digit counts trip people up. Amazon reported it in September 2026. If you can trace 3[a2[c]] by hand, you can code it in about fifteen minutes.

What's the trick to solving it?+

Push the current string and repeat count onto a stack at every open bracket, then reset. At a close bracket, pop and combine: previous string plus current string times the count. Letters just append to the current string.

What's the most common mistake?+

Reading digits one at a time. A count like 25 must be built as num * 10 + digit. The second mistake is not resetting the number after pushing it, which corrupts the next block.

Should I use a stack or recursion?+

Either passes. The stack is easier to debug under pressure because the state is explicit. Recursion with a shared index is shorter but easier to get wrong when the index moves across nested calls. Pick the one you can write without thinking.

How do I prepare in 48 hours?+

Write the stack version once from memory, then trace both examples by hand. Add a test with a multi-digit count like 10[a] and one with adjacent blocks. Check that you join lists instead of concatenating strings in loops.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it