Count the Ways to Decode a Digit-Only Message

Last updated: November 29, 2026

Quick Overview

A message of letters was encoded with A=1 through Z=26 and the separators were lost; given the digit string, return how many distinct letter strings it could decode to.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A message of letters was encoded with A=1 through Z=26 and the separators were lost; given the digit string, return how many distinct letter strings it could decode to.

A Google technical screen classic for testing one-dimensional dynamic programming with tricky validity rules. The interviewer is watching how you handle zeros, which only decode as part of 10 or 20, and whether you compress the DP to O(1) space.

What the Interviewer Expects
  • Defines the DP state clearly as ways to decode a prefix
  • Handles '0' correctly as invalid alone
  • Checks two-digit codes in the range 10 to 26 only
  • Reduces space to two rolling variables
  • Traces a zero-heavy example
Key Topics to Cover
Dynamic Programming
Strings
Counting
Space Optimization
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • The message may contain '*' meaning any digit 1 to 9; count modulo 10^9 + 7.
  • Return one decoding, or all decodings, instead of the count.
  • How would you decode a stream of digits with bounded memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: string of digits, possibly empty or with leading zeros.
  • A single digit 1 to 9 decodes alone; a pair 10 to 26 decodes together.
  • Output: count of decodings (0 if none)....
High-Level Design

Let dp[i] be the number of ways to decode the first i characters, with dp[0] = 1. For position i, if the single digit s[i-1] is not '0', add dp[i-1]. If the two-digit number s[i-2:i] is betw...


Submit Your Answer
Markdown supported