Minimum-Cost Digit String Decoding
Reported by candidates from SquadStack.ai's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The SquadStack.ai OA reported in September 2026 looks like a string problem, but it's really Decode Ways with a price tag. Instead of counting splits, you pick the cheapest one. Each position either takes a one-digit chunk or a two-digit chunk, and you carry the best cost forward. If you've got an OA invite and 48 hours, this is a 15-line DP once you see it. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment, but the idea below should get you most of the way.
The problem
You are given a non-empty digit string digits and an array letterCosts of length 26. The integers 1 through 26 represent the letters A through Z. The cost of decoding a chunk as letter x is letterCosts[x - 1]. Split digits into valid one- or two-digit chunks. A chunk is valid when: it has no leading zero, and its integer value is between 1 and 26, inclusive. The cost of a decoding is the sum of its decoded-letter costs. Return the minimum cost among all valid decodings. If no valid decoding exists, return -1. Function minDecodingCost(digits: String, letterCosts: int[]) → long Examples Example 1 digits = "12" letterCosts = [5,6,20,20,20,20,20,20,20,20,20,4,20,20,20,20,20,20,20,20,20,20,20,20,20,20] return = 4 Decoding "12" as A, B costs 5 + 6 = 11. Decoding it as L costs 4, which is smaller. Example 2 digits = "226" letterCosts = [50,3,50,50,50,8,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,4,50,50,50,10] return = 12 The valid splits are 2|2|6 with cost 14, 22|6 with cost 12, and 2|26 with cost 13. The minimum is 12. Example 3 digits = "06" letterCosts = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] return = -1 A chunk cannot start with 0, so the string has no valid decoding. Constraints 1 <= digits.length <= 10^5. digits contains only the characters '0' through '9'. letterCosts.length == 26. 0 <= letterCosts[i] <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a linear DP. Let dp[i] be the minimum cost to decode the first i characters, with dp[0] = 0 and everything else infinity. At position i, if digits[i-1] isn't '0', try dp[i-1] + letterCosts[digit-1]. If i >= 2, the two-digit value from digits[i-2..i-1] has no leading zero and sits between 10 and 26, try dp[i-2] + letterCosts[value-1]. Take the min. At the end, infinity means return -1. The pitfalls are real. Zeros kill a one-digit chunk, and values like 05 or 30 are invalid as two-digit chunks. Use long, because 10^5 chunks at 10^9 each overflows int. Don't use recursion without memoization, since the length goes to 10^5 and you'll blow the stack. If you freeze on the live OA, StealthCoder can hand you the recurrence, but you can rebuild it from the examples.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Minimum-Cost Digit String Decoding 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass SquadStack.ai's OA.
SquadStack.ai reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum-Cost Digit String Decoding FAQ
What's the trick in the SquadStack.ai minimum-cost decoding problem?+
It's Decode Ways with costs. Use a DP over prefixes where dp[i] is the cheapest cost to decode the first i digits. Each step looks back one or two characters, validates the chunk, and takes the minimum. Unreachable states stay at infinity and become -1.
How hard is this problem really?+
Medium at most. The DP is one-dimensional and the transitions are two lines. The difficulty is in edge cases: zeros, leading-zero pairs like 06, and overflow. If you've seen Decode Ways, you've mostly seen this.
Why do I need long instead of int?+
The string can be 10^5 digits and each letter cost can reach 10^9. The total can reach 10^14, which overflows a 32-bit int. Use long for the dp array and use a large sentinel like Long.MAX_VALUE / 2 for infinity to avoid overflow when adding.
When does the answer come out as -1?+
When no valid split exists. That happens with a leading 0, or a 0 not preceded by a 1 or 2, such as 30 or 100 in the wrong spot. In the DP, dp[n] stays at infinity, and you return -1.
How do I prepare for this in 48 hours?+
Write Decode Ways from scratch, then add the cost array and the infinity handling. Test with 12, 226, and 06 from the examples. Then try inputs like 100, 27, and 10 to confirm zero handling. Convert to a rolling two-variable version if you want O(1) space.