Reported March 2022
SambaNova Systemsstack

Decode String

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

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

The whole problem hinges on a stack. SambaNova Systems reported this Decode String question in March 2022, and if you've got an OA coming up, expect nested brackets like 3[a2[c]] to be the part that trips people. The input is a valid encoded string, and you return the fully expanded version. It's a classic string problem with a parser flavor. Nothing exotic, but the nesting punishes sloppy bookkeeping. If your mind goes blank on the live assessment, StealthCoder sits invisibly on your desktop and hands you a working solution. Know the shape of the answer first, though.

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

Use a stack to save state when you hit an opening bracket. Scan left to right. Build the current number digit by digit (counts can be multi-digit, up to 300). Build the current string from letters. On '[', push the current string and the current number, then reset both. On ']', pop the previous string and the count, then set current to previous plus current repeated count times. At the end, current is your answer. The common pitfall is treating the count as a single digit, so 12[a] breaks. Another is forgetting to reset the number after pushing. Output is capped at 10^5, so repeated concatenation is fine, but use a list and join if you want to be safe. If you freeze mid-assessment, StealthCoder is the hedge that reads the problem and gives you the stack solution in real time.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

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. If you're reading this with an OA window open, you're who this was built for.

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 SambaNova Systems's OA.

SambaNova Systems reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Decode String FAQ

What's the trick in Decode String?+

Use a stack to remember the string built so far and the repeat count whenever you hit '['. On ']', pop both and append the current segment repeated count times to the saved string. That handles any nesting depth in one pass.

How hard is this problem really?+

Medium. The idea is short once you see the stack, but edge cases bite: multi-digit counts, nested groups, and literal text after a closing bracket. If you've written a bracket matcher before, you'll get it done fast.

Can I solve it recursively instead?+

Yes. Write a helper that parses until it hits ']' or the end, returning the decoded piece and the new index. It's the same logic as the stack, using the call stack. With length up to 10^5, deep nesting could risk recursion limits, so the explicit stack is safer.

What's the time complexity?+

It's linear in the size of the output plus the input, since each character is processed once and each expansion copies its content. With decoded length capped at 10^5, that's comfortably fast. Space is also bounded by the output and nesting depth.

How do I prepare in 48 hours for this OA from SambaNova Systems?+

Hand-trace 3[a2[c]] and 2[abc]3[cd]ef on paper with a stack until it feels automatic. Then code it twice from scratch, once with a stack, once recursively. Test a two-digit count like 10[a] and nested brackets with trailing letters.

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

OA at SambaNova Systems?
Invisible during screen share
Get it