Count Stepping Numbers In Range
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reportedly served this one in September 2026, and it looks friendlier than it is. Count stepping numbers between low and high, where every adjacent digit pair differs by exactly 1. With bounds up to 10^15, looping through the range is dead on arrival. Underneath, it's a counting problem: build or count valid digit strings instead of testing numbers one by one. If you blank on the approach during the OA, StealthCoder is the safety net running invisibly on your screen. But the trick is short enough to own before you sit down.
The problem
A stepping number is an integer whose adjacent digits differ by exactly 1. Every one-digit integer, including 0, is a stepping number. Given two integers low and high, return the number of stepping numbers in the inclusive range [low, high]. Function countSteppingNumbers(low: long, high: long) → int Examples Example 1 low = 0 high = 21 return = 13 The stepping numbers in [0, 21] are 0 through 9, plus 10, 12, and 21. Example 2 low = 10 high = 15 return = 2 Only 10 and 12 are stepping numbers in [10, 15]. Constraints 0 <= low <= high <= 10^15.
Reported by candidates. Source: FastPrep
Pattern and pitfall
There are two clean routes. First, BFS generation: start from digits 1 through 9, and from each number append last digit plus or minus 1 (when it stays within 0 to 9). Stop expanding once a number exceeds high. Stepping numbers are sparse, so this finishes fast even at 10^15, and you count values in [low, high], plus 0 if low is 0. Second, digit DP: count(n) of stepping numbers up to n, then answer count(high) minus count(low-1), tracking position, previous digit, tight flag, and started flag. Common pitfalls: forgetting 0 is a stepping number, letting a leading zero break the adjacency check, overflow (use 64-bit), and low-1 going negative when low is 0. BFS is simpler to write under pressure. If the live OA freezes your brain, StealthCoder can hand you the working version.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Stepping Numbers In Range cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as stepping numbers. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Stepping Numbers In Range FAQ
What's the trick to Count Stepping Numbers In Range?+
Don't iterate the range. Stepping numbers are rare, so generate them. Start from 1 through 9, extend each by last digit minus 1 and plus 1, and prune anything above high. Count the ones that land in [low, high]. Add 0 separately if low is 0.
Should I use BFS or digit DP for this Amazon OA?+
BFS is faster to code and less error-prone. The count of stepping numbers up to 10^15 is small enough to enumerate. Digit DP is cleaner theoretically but has more state to get wrong: tight flag, leading zeros, previous digit. Pick BFS unless you already know digit DP cold.
What edge cases break most solutions?+
Zero is the big one. It's a stepping number but BFS seeded from 1 through 9 never produces it. Also watch low equal to 0 in the DP route, since low-1 goes negative. Single-digit ranges and high at 10^15 should be tested too. Use 64-bit integers throughout.
What's the time complexity?+
For BFS, it's proportional to the number of stepping numbers up to high, which is small. Each number has at most two children, and length is at most 16 digits. For digit DP, it's about digits times 10 times a few flags. Both fit comfortably for 10^15.
How do I prepare for this in 48 hours?+
Write the BFS version from scratch twice, then test it on the two examples: [0, 21] gives 13 and [10, 15] gives 2. Then skim digit DP for the general shape. Reported for Amazon in September 2026, so expect a counting-with-constraints style problem, not a trick question.