Alphanumeric Combinations

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

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

You've got a Goldman Sachs OA in July with a decoding problem. Given a string of digits, you need to count how many ways it can be split into valid letter mappings (1-26 for a-z). The trick is that '0' is invalid on its own and can only pair with preceding digits. This is a classic dynamic programming problem where order matters and invalid splits kill entire branches.

The problem

John is teaching his son Rob English alphabet and number counting. He reperesents 'a' as the 1st character, 'b' as the 2nd character up to 'z' as the 26th character. John says 'kite' can be representd as '119205'. The 11th character is 'k', 9th character is 'i', 20th character is 't' and 5th character is 'e'. Rob being smarter than him, says '119205' can also mean 'aaite' (1)(1)(9)(20)(5), 'aste'(1)(19)(20)(5), etc. Now being enthusiastic about it, John wants to know given a string os length n containing digits from 0 to 9, how many such words are possible. Input Format You are given a string S containing characters from 0-9. You have to find how many such words are possible for that given number sequence, Output Format A single integer returning the number of words. If there are no possible combinations output will be 0. 🪻ଓ༉ Credit to ᡣ𐭩Full Stack Devᡣ𐭩 𓇢𓆸

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is dynamic programming with string segmentation. At each position, you can either take one digit (if it maps 1-9) or two digits (if the pair maps 10-26). A '0' can't stand alone. Build a DP array where dp[i] represents valid decodings up to index i. For each position, check if the single digit is valid, then check if the last two digits form a valid pair. The catch: leading zeros in multi-digit numbers (like '01') are dead ends. StealthCoder will handle the index boundary checks and zero-validation logic if you blank on the state transitions during the live OA.

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 Alphanumeric Combinations 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 ways. If you have time before the OA, drill that.

⏵ The honest play

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

Goldman Sachs 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.

Alphanumeric Combinations FAQ

What makes this different from other DP string problems?+

The zero rule. A '0' can never decode to anything by itself. It must be part of '10' or '20'. Most candidates forget to check if dp[i-2] is valid before using pairs containing zero, causing wrong answers on edge cases.

Is this really just counting subsets?+

No. It's partitioning a string into contiguous non-overlapping segments, each representing a valid digit (1-9) or pair (10-26). Order and adjacency matter. You're not choosing which digits to use, you're deciding where to split.

What input trips people up?+

Strings like '10', '101', '1001', and anything with consecutive zeros or leading zeros in pairs. '00' has zero valid decodings. '10' has exactly one (1, 0 is invalid so only the pair 10 works). Test these mentally before submitting.

Do I need memoization or bottom-up DP?+

Either works, but bottom-up is cleaner here. You only need to track the last two DP values as you iterate forward. Space optimization is easy. Time is O(n), space is O(1) if you're smart about it.

How do I know if my answer is right?+

Trace '119205' by hand. The valid splits are: (1,1,9,20,5), (1,19,20,5), (11,9,20,5), (11,92,0,5) nope zero fails, (1,1,92,0,5) nope. Should get 3. If you get a different number, your zero-handling is broken.

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

OA at Goldman Sachs?
Invisible during screen share
Get it