DSA With AI
Week 7 ยท 1-D Dynamic Programming

Decode Ways

Solve Decode Ways by recognizing the 1-D Dynamic Programming pattern and turning the prompt into a small invariant before coding.

Medium 1-D Dynamic Programming Week 7 Practice runner

Frame the problem

  • Implement num_decodings with the exact signature used by the interactive runner.
  • Use the visible tests to confirm the input and output shape before reading the final solution.
  • Treat challenge tests as edge-case pressure: empty inputs, repeated values, boundary shapes, or impossible states.
  • State the invariant before code, then dry-run one passing case and one failing-looking case.
1. Reveal example inputs and outputs

Example 1

Input:

num_decodings("226")

Output:

3
2. Brute force first

What direct brute force would be correct for a tiny input? Name the exact repeated work that the target pattern removes.

3. Reveal the insight ladder
  1. Map the prompt to the 1-D Dynamic Programming pattern instead of starting from syntax.
  2. Each position can use a one-digit decode and sometimes a two-digit decode.
  3. A leading zero has no valid letter.
  4. Only reveal the final code after you can explain why each state update is safe.
4. Dry run before code
  1. decode-basic: input ["226"] should produce 3. Hint to check your state: Each position can use a one-digit decode and sometimes a two-digit decode.
  2. decode-zero: input ["06"] should produce 0. Hint to check your state: A leading zero has no valid letter.
5. Reveal final Python solution
def num_decodings(s: str) -> int:
    if not s or s[0] == "0":
        return 0
    prev2, prev1 = 1, 1
    for index in range(1, len(s)):
        current = 0
        if s[index] != "0":
            current += prev1
        if 10 <= int(s[index - 1 : index + 1]) <= 26:
            current += prev2
        prev2, prev1 = prev1, current
    return prev1

Complexity: Derive the exact bounds from num_decodings: count how often each input item is visited and the maximum size of the main state structure.

Interview narration

  • I will first describe the invariant in plain language.
  • Then I will explain what data structure carries that invariant across the traversal, loop, recursion, or DP transition.
  • Finally I will walk one edge case before writing the optimized version.

Common traps

  • Solving only the visible example instead of the invariant.
  • Forgetting empty input, singleton input, duplicate values, or impossible-state cases.
  • Revealing the solution before doing a dry run from the starter signature.

Follow-up drills

1. How do you turn this into a timed interview answer?

Start with the invariant, give the brute force in one sentence, name the optimized state, code the core loop or recursion, and run one visible test aloud before mentioning complexity.

2. How do you scale the same pattern to a larger input?

Track which state grows with the input: hash maps and sets grow with distinct values, queues grow with frontier width, recursion grows with depth, heaps grow with active candidates, and DP tables grow with state count.

3. What should you practice from blank tomorrow?

Rewrite num_decodings without looking at the solution, then compare only the invariant and state updates before checking syntax.

Interactive runner

Write the required Python function. Your code runs locally in this browser. Hints reveal one failing case at a time.