PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

Count decodings of a digit string evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Morgan Stanley
  • Coding & Algorithms
  • Software Engineer

Count decodings of a digit string

Company: Morgan Stanley

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Take-home Project

You are given a non-empty digit string s consisting of characters '0'–'9' and a mapping where 1->'a', 2->'b', ..., 26->'z'. Return the number of valid ways to decode s. Note that '0' cannot be decoded alone and is only valid as part of '10' or '20'. Provide an O(n) time solution, explain how to handle zeros and invalid prefixes, analyze time and space complexity, and implement in C++. Optionally include a space-optimized variant.

Quick Answer: Count decodings of a digit string evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

A message containing letters A-Z is encoded to digits using the mapping 1->'a', 2->'b', ..., 26->'z'. Given a non-empty digit string `s` consisting of characters '0'-'9', return the number of valid ways to decode it back into letters. Rules: - A single digit '1'-'9' decodes to one letter. - A two-digit group decodes to a letter only when its value is between 10 and 26 inclusive. - The digit '0' cannot stand alone; it is only valid as the second digit of '10' or '20'. Any other appearance of '0' (e.g. a leading '0', or '0' preceded by a digit > 2) makes that prefix undecodable. If the string cannot be decoded at all, return 0. Aim for O(n) time and O(1) extra space.

Constraints

  • 1 <= len(s) <= 100
  • s consists only of characters '0'-'9'
  • Return 0 when s is undecodable (e.g. leading '0', or '0' not following '1' or '2')

Examples

Input: ("12",)

Expected Output: 2

Explanation: "12" decodes as 'ab' (1,2) or 'l' (12), giving 2 ways.

Input: ("226",)

Expected Output: 3

Explanation: "226" -> 'bbf'(2,2,6), 'vf'(22,6), 'bz'(2,26): 3 ways.

Input: ("06",)

Expected Output: 0

Explanation: A leading '0' has no decoding, so the answer is 0.

Input: ("10",)

Expected Output: 1

Explanation: '0' is only valid as part of '10' here, which decodes to 'j'; exactly 1 way.

Input: ("100",)

Expected Output: 0

Explanation: After decoding '10', the trailing '0' cannot be decoded (not preceded by 1 or 2), so 0 ways.

Input: ("27",)

Expected Output: 1

Explanation: 27 > 26, so only the single-digit split 2,7 works: 'bg', 1 way.

Input: ("11106",)

Expected Output: 2

Explanation: "11106" -> 'aaj'(1,1,10,...) wait: valid splits are (1,1,10,6)->'aaj f' style and (11,10,6); '06' alone is invalid so only paths routing the '0' through '10' survive, giving 2 ways.

Hints

  1. Let dp[i] be the number of ways to decode the prefix s[0..i-1]. Each step you either consume one digit or two digits, so dp[i] depends only on dp[i-1] and dp[i-2] — a perfect fit for a rolling two-variable DP (O(1) space).
  2. A single digit contributes dp[i-1] only when s[i-1] != '0'. A two-digit group s[i-2..i-1] contributes dp[i-2] only when its numeric value is between 10 and 26.
  3. Handle '0' carefully: a leading '0' is immediately undecodable, and any '0' that is neither '10' nor '20' kills that path. If at any position the running count becomes 0, the whole string is undecodable, so return 0.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Factorial and Squares - Morgan Stanley (medium)
  • Compute maximum non-overlapping meetings - Morgan Stanley (medium)
  • How do you escape the circle? - Morgan Stanley (medium)
  • Compute win probability in coin-toss game - Morgan Stanley (easy)
  • Compute minimal cost to merge numbers - Morgan Stanley (Medium)