The Decode String interview question asks you to expand a compressed string format. The encoding rule is k[encoded_string], which means the encoded_string inside the square brackets is being repeated exactly k times. k is guaranteed to be a positive integer. For example, 3[a]2[bc] should be decoded as aaabcbc. This Decode String coding problem involves handling nested structures, like 2[a3[b]].
This is an incredibly popular question at companies like Google, Bloomberg, and Adobe. it tests your proficiency with Stacks or Recursion. It evaluates how you handle nested logic and your ability to process tokens (numbers, brackets, and characters) in a single pass. It’s a mini-version of an expression parser or a compiler.
This utilizes the Recursion, String, Stack interview pattern.
Input: 3[a2[c]]
Practice both the stack-based and the recursive solutions. The recursive solution is often shorter and more elegant, while the stack-based one is sometimes easier to reason about for large inputs.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Ternary Expression Parser | Medium | Solve | |
| Parsing A Boolean Expression | Hard | Solve | |
| Basic Calculator | Hard | Solve | |
| Basic Calculator III | Hard | Solve | |
| Parse Lisp Expression | Hard | Solve |