The "Count Stepping Numbers in Range interview question" is a digit-based counting challenge. A "stepping number" is an integer where the absolute difference between any two adjacent digits is exactly 1. For example, 123, 432, and 565 are stepping numbers, but 124 is not. You are given two numeric strings, low and high, and need to count how many stepping numbers exist in the inclusive range [low, high].
Amazon ask the "Count Stepping Numbers coding problem" to test a candidate's ability to solve "Digit DP" problems. These are a special class of "Dynamic Programming interview pattern" tasks where you count numbers satisfying a property by building them digit by digit. It evaluates your mastery of state-based counting and handling large number ranges that exceed 64-bit integer limits.
This problem is solved using Digit Dynamic Programming.
count(low, high) = count(high) - count(low - 1).dp(index, prev_digit, is_less, is_started)
index: Current digit position we are filling.prev_digit: The digit placed at index-1.is_less: Boolean indicating if we are already smaller than the prefix of the limit string.is_started: Boolean indicating if we have placed any non-zero digits yet.is_started is true, must satisfy .Range [10, 20]
low - 1 when low is a string of length 100. It's often easier to check if low itself is a stepping number and then use count(high) - count(low).is_started flag correctly, which leads to incorrect stepping logic for numbers with fewer digits than the limit.is_less) in the DP table.Practice "Digit DP" templates. They are remarkably consistent: they almost always involve a recursive function that builds a number from left to right while tracking boundaries and properties. Once you master the template, "Hard" problems like this become much more approachable.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Distinct Subsequences II | Hard | Solve | |
| Valid Palindrome III | Hard | Solve | |
| Count Different Palindromic Subsequences | Hard | Solve | |
| String Compression II | Hard | Solve | |
| Shortest Common Supersequence | Hard | Solve |