Sequential Digits - Problem

Imagine a special number where each digit follows a perfect sequence - each digit is exactly one more than the previous digit! These are called sequential digits.

For example:

  • 123 has sequential digits: 1→2→3 (each increases by 1)
  • 89 has sequential digits: 8→9 (increases by 1)
  • 1234567 has sequential digits: 1→2→3→4→5→6→7
  • 132 does NOT have sequential digits (3→2 decreases)

Given a range [low, high], your task is to find all numbers with sequential digits within this range and return them in sorted order.

Goal: Return a sorted list of all integers in the range [low, high] that have sequential digits.

Input & Output

example_1.py — Basic Range
$ Input: low = 100, high = 300
Output: [123, 234]
💡 Note: In the range [100, 300], only 123 (1→2→3) and 234 (2→3→4) have sequential digits. Both numbers have each digit exactly one more than the previous digit.
example_2.py — Small Range
$ Input: low = 1000, high = 13000
Output: [1234, 2345, 3456, 4567, 5678, 6789, 12345]
💡 Note: All 4-digit sequential numbers (1234 through 6789) fall in this range, plus one 5-digit number (12345). Each follows the pattern where digits increase by exactly 1.
example_3.py — Edge Case
$ Input: low = 10, high = 20
Output: [12]
💡 Note: Only the 2-digit number 12 falls in this small range. Single digits (10) don't count as sequential since we need at least two digits to form a sequence.

Visualization

Tap to expand
Sequential Digits Generation Tree112123123422323499?Can't extendGeneration Pattern• Start with digits 1-9 (single digit numbers are valid)• Extend each number: next_num = current_num × 10 + (last_digit + 1)• Stop when: next_digit > 9 OR next_num > high• Collect all numbers where low ≤ number ≤ highWhy This Works: Limited PossibilitiesMaximum sequential numbers: 9+8+7+6+5+4+3+2+1 = 45 total
Understanding the Visualization
1
Plant Seeds
Start with single digits 1-9 as root nodes
2
Grow Branches
From each digit, extend by adding next consecutive digit
3
Prune Invalid
Stop growing when next digit > 9 or number > high
4
Harvest Results
Collect all numbers within [low, high] range
5
Sort Output
Return naturally ordered sequential numbers
Key Takeaway
🎯 Key Insight: Instead of checking every number in a potentially huge range, we generate only the limited set of valid sequential digit numbers (at most 45), making this approach extremely efficient with O(1) time complexity!

Time & Space Complexity

Time Complexity
⏱️
O(1)

There are at most 45 sequential digit numbers possible (limited by digit constraints), so we generate a constant number of candidates

n
2n
Linear Growth
Space Complexity
O(1)

Space used is proportional to the output size, which is bounded by a small constant

n
2n
Linear Space

Constraints

  • 10 ≤ low ≤ high ≤ 109
  • All integers in the range must be checked for sequential digit property
  • Sequential digits: each digit is exactly one more than the previous digit
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
89.2K Views
Medium Frequency
~15 min Avg. Time
1.8K 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