Stepping Numbers - Problem

A stepping number is a fascinating mathematical concept where each digit in the number has a very special relationship with its neighbors. Specifically, every pair of adjacent digits must have an absolute difference of exactly 1.

Think of it like climbing stairs - you can only go up or down by exactly one step at a time! For example:

  • 321 is a stepping number: |3-2| = 1, |2-1| = 1 ✓
  • 4321 is a stepping number: |4-3| = 1, |3-2| = 1, |2-1| = 1 ✓
  • 421 is NOT a stepping number: |4-2| = 2 ✗

Your task is to find all stepping numbers within a given range [low, high] (inclusive) and return them in sorted order.

Goal: Generate all valid stepping numbers in the range efficiently.
Input: Two integers low and high representing the inclusive range.
Output: A sorted list of all stepping numbers in the range.

Input & Output

example_1.py — Basic Range
$ Input: low = 0, high = 21
Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21]
💡 Note: All single digits (0-9) are stepping numbers. For two digits: 10 (|1-0|=1), 12 (|1-2|=1), 21 (|2-1|=1) are valid. Numbers like 11 (|1-1|=0) and 20 (|2-0|=2) are not stepping numbers.
example_2.py — Larger Range
$ Input: low = 10, high = 15
Output: [10, 12]
💡 Note: In range [10,15]: 10 is valid (|1-0|=1), 12 is valid (|1-2|=1), but 11 (|1-1|=0), 13 (|1-3|=2), 14 (|1-4|=3), 15 (|1-5|=4) are all invalid.
example_3.py — Edge Case
$ Input: low = 0, high = 0
Output: [0]
💡 Note: Single digit 0 is considered a stepping number by definition (no adjacent digits to compare).

Visualization

Tap to expand
The Stepping Number Staircase BuilderFoundation: Single Digits123... 9Level 2: Valid Extensions10122123...Level 3: Longer Staircases101121123210Building Rules🔸 From digit d, can append:• (d-1) if d > 0• (d+1) if d < 9🔸 Example: From 12• Last digit is 2• Can append 1 → 121• Can append 3 → 123
Understanding the Visualization
1
Foundation Level
Start with single-digit staircases (1,2,3,4,5,6,7,8,9) - these are our building blocks
2
Build Second Floor
From each single digit, add steps that are exactly ±1: from 1 we can build 10 or 12
3
Expand Systematically
Continue building higher floors: from 10 we can build 101, from 12 we can build 121 or 123
4
Filter by Range
Collect only staircases that fit within our required height range [low, high]
Key Takeaway
🎯 Key Insight: Generate only valid stepping numbers using BFS rather than testing every number in the range - this transforms an O(n) checking problem into an O(k) generation problem where k << n!

Time & Space Complexity

Time Complexity
⏱️
O(k * log(high))

Where k is the number of stepping numbers ≤ high. We generate each valid number once.

n
2n
Linearithmic
Space Complexity
O(k)

Space for the queue and result array containing k stepping numbers

n
2n
Linear Space

Constraints

  • 0 ≤ low ≤ high ≤ 2 × 109
  • The answer is guaranteed to fit within a 32-bit integer
  • Single digits (0-9) are considered stepping numbers
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 23
38.5K Views
Medium Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen