Count Ways to Decode a Compressed Shipment Tracking Code

Last updated: November 29, 2026

Quick Overview

A shipping partner compresses letter codes by mapping A to 1, B to 2, up to Z to 26, and concatenating the numbers without separators. Given the digit string, return the number of ways it can be decoded back into letters, modulo 1,000,000,007.

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

0

0

0 solved


A shipping partner compresses letter codes by mapping A to 1, B to 2, up to Z to 26, and concatenating the numbers without separators. Given the digit string, return the number of ways it can be decoded back into letters, modulo 1,000,000,007.

A one-dimensional DP question in the style of Amazon technical screens, set in carrier data exchange. The interviewer checks the recurrence, careful handling of zeros, and reducing DP space to two variables.

What the Interviewer Expects
  • Define dp[i] as ways to decode the first i digits
  • Handle single-digit and two-digit transitions with zero rules
  • Reduce to O(1) space
  • Apply the modulus at each step
  • Test strings with leading zeros and '10', '20', '30'
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
  • How would you return one valid decoding or all of them?
  • What if '*' can stand for any digit 1 to 9?
  • How would you validate a code before decoding?
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 is a string of digits, possibly empty (return 0 for empty, since there is nothing to decode). '0' alone cannot be decoded; '10' and '20' decode only as two-digit letters. Return count mod 1e9+7....

High-Level Design

dp[0] = 1 (empty prefix), and for each position i (1-indexed): if s[i-1] != '0' then dp[i] += dp[i-1]; if the two-digit number s[i-2..i-1] is between 10 and 26, then dp[i] += dp[i-2]. Only the last tw...


Submit Your Answer
Markdown supported