Decode Ways II

Algorithms
Hard
Amazon
119.6K views

An encoded message contains digits and the character '*'. '*' can represent any digit from 1 to 9. Determine the total number of ways to decode the message. Harder DP than 'Decode Ways I'.

Why Interviewers Ask This

Amazon interviewers ask this to evaluate your mastery of dynamic programming under constraints. Specifically, they test if you can handle ambiguity with the wildcard '*' while managing modular arithmetic to prevent integer overflow. This mirrors Amazon's Leadership Principle of 'Dive Deep,' requiring candidates to analyze edge cases like leading zeros and multi-digit combinations rigorously before coding.

How to Answer This Question

1. Clarify constraints immediately: Confirm input length limits and whether the result should be modulo 10^9 + 7. 2. Define state transitions: Explain that dp[i] depends on single digit decoding (s[i]) and two-digit decoding (s[i-1..i]). 3. Handle the wildcard logic: Detail how '*' acts as 1-9 for single digits and interacts with previous characters for pairs (e.g., '*1' creates 11 or 21). 4. Optimize space: Propose using O(1) space by tracking only the last two states instead of a full array. 5. Walk through an example: Trace '1*' step-by-step to show how counts multiply. 6. Address overflow: Explicitly mention applying modulo at every addition step to ensure safety.

Key Points to Cover

  • Explicitly handling the '*' wildcard logic for both single and double-digit combinations
  • Applying modulo arithmetic at every addition step to prevent integer overflow
  • Optimizing space complexity from O(n) to O(1) by tracking only necessary previous states
  • Clearly distinguishing between valid two-digit ranges (10-19 vs 20-26)
  • Demonstrating ability to trace edge cases like consecutive wildcards or leading zeros

Sample Answer

To solve Decode Ways II, I first clarify the constraints, noting the need for modulo 10^9 + 7 due to potential exponential growth. The core strategy is dynamic programming where dp[i] represents ways to decode the prefix ending at index i. We initialize dp[0] = 1. For each character, we calculate contributions from taking it alone and combining it with the previous character. If s[i] is a digit, it contributes based on its value; if it's '*', it adds 9 possibilities. Crucially, the two-digit check requires careful branching. For instance, if s[i-1] is '1', s[i] can be any digit 0-9. If s[i-1] is '2', s[i] is limited to 0-6. With wildcards, s[i-1]='*' and s[i]='*' yields 15 valid pairs (11-19 and 21-26). I will implement this with O(1) space optimization, maintaining just prev2 and prev1 variables. I'll trace '1*' manually: '1' gives 1 way, '*' gives 9 ways alone, plus '1*' forms 11-19 (9 ways), totaling 18. Finally, I ensure every addition applies the modulo operator to prevent overflow, which is critical for Amazon's large-scale data handling expectations.

Common Mistakes to Avoid

  • Forgetting to apply the modulo operator after every addition, causing integer overflow in large inputs
  • Incorrectly counting invalid two-digit combinations when wildcards are involved, such as allowing 27-29
  • Ignoring the case where the previous character is '0', which cannot form a valid two-digit number
  • Using O(n) space without realizing the problem can be solved with constant space optimization

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 73 Amazon questions