Magicsheet logo

Sequential Digits

Medium
12.5%
Updated 8/1/2025

Sequential Digits

What is this problem about?

The Sequential Digits interview question asks you to find all integers in the range [low, high] whose digits form a sequential run — each digit is exactly one more than the previous digit. For example, 123, 234, 456789 are sequential-digit numbers. 321 and 135 are not. Return all such numbers in sorted order within the given range.

Why is this asked in interviews?

Apple, Uber, Meta, Amazon, and Google ask this problem because it tests enumeration on a small, well-defined search space. There are only 36 sequential-digit numbers in total (since digits go from 1 to 9, and you can start at any digit with any length from 2 to 9 digits). Generating them all, filtering by range, and returning sorted — rather than scanning the full range — is the key algorithmic insight. It rewards candidates who recognize the enumeration pattern over brute-force iteration.

Algorithmic pattern used

The pattern is complete enumeration of the bounded search space. Generate all sequential-digit numbers systematically: for each starting digit d from 1 to 9, and for each length l from 2 to 10 - d (so the last digit doesn't exceed 9), build the number by appending consecutive digits. Collect all generated numbers that fall within [low, high]. Sort the result (or generate in sorted order by iterating shorter lengths first). This yields at most 36 candidates — extremely fast.

Example explanation

Generate all sequential-digit numbers:

  • Length 2: 12, 23, 34, 45, 56, 67, 78, 89.
  • Length 3: 123, 234, 345, 456, 567, 678, 789.
  • Length 4: 1234, 2345, 3456, 4567, 5678, 6789.
  • ... up to length 9: 123456789.

Filter for low=100, high=300: Candidates in range: 123, 234. Result: [123, 234].

Common mistakes candidates make

  • Iterating through all numbers from low to high and checking each — O(high - low) can be up to 10^9, far too slow.
  • Generating duplicate numbers by not properly bounding the starting digit and length.
  • Not sorting the final result — numbers must be returned in ascending order.
  • Starting sequential digits from 0 — sequential digit numbers start from 1 (digits 1–9 only).

Interview preparation tip

For the Sequential Digits coding problem, the enumeration interview pattern is the correct approach. The insight: the total space of valid answers is tiny (36 numbers maximum), so generate all candidates and filter. Interviewers at Apple and Meta appreciate when you state the bounded search space size upfront — it immediately signals you won't be scanning billions of numbers. Practice this "generate all valid candidates, then filter" pattern on problems with small, well-structured answer spaces — it avoids over-engineering with complex binary search or DP.

Similar Questions