Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300 Output: [123,234]
Example 2:
Input: low = 1000, high = 13000 Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9Problem summary: An integer has sequential digits if and only if each digit in the number is one more than the previous digit. Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
100 300
1000 13000
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1291: Sequential Digits
class Solution {
public List<Integer> sequentialDigits(int low, int high) {
List<Integer> ans = new ArrayList<>();
for (int i = 1; i < 9; ++i) {
int x = i;
for (int j = i + 1; j < 10; ++j) {
x = x * 10 + j;
if (x >= low && x <= high) {
ans.add(x);
}
}
}
Collections.sort(ans);
return ans;
}
}
// Accepted solution for LeetCode #1291: Sequential Digits
func sequentialDigits(low int, high int) (ans []int) {
for i := 1; i < 9; i++ {
x := i
for j := i + 1; j < 10; j++ {
x = x*10 + j
if low <= x && x <= high {
ans = append(ans, x)
}
}
}
sort.Ints(ans)
return
}
# Accepted solution for LeetCode #1291: Sequential Digits
class Solution:
def sequentialDigits(self, low: int, high: int) -> List[int]:
ans = []
for i in range(1, 9):
x = i
for j in range(i + 1, 10):
x = x * 10 + j
if low <= x <= high:
ans.append(x)
return sorted(ans)
// Accepted solution for LeetCode #1291: Sequential Digits
struct Solution;
impl Solution {
fn sequential_digits(low: i32, high: i32) -> Vec<i32> {
let mut res = vec![];
for i in 1..10 {
Self::dfs(i, i, &mut res, low, high);
}
res.sort_unstable();
res
}
fn dfs(last_digit: i32, cur: i32, all: &mut Vec<i32>, low: i32, high: i32) {
if cur >= low && cur <= high {
all.push(cur);
}
if cur >= high {
return;
}
if last_digit < 9 {
Self::dfs(last_digit + 1, cur * 10 + last_digit + 1, all, low, high);
}
}
}
#[test]
fn test() {
let low = 100;
let high = 300;
let res = vec![123, 234];
assert_eq!(Solution::sequential_digits(low, high), res);
let low = 1000;
let high = 13000;
let res = vec![1234, 2345, 3456, 4567, 5678, 6789, 12345];
assert_eq!(Solution::sequential_digits(low, high), res);
}
// Accepted solution for LeetCode #1291: Sequential Digits
function sequentialDigits(low: number, high: number): number[] {
const ans: number[] = [];
for (let i = 1; i < 9; ++i) {
let x = i;
for (let j = i + 1; j < 10; ++j) {
x = x * 10 + j;
if (x >= low && x <= high) {
ans.push(x);
}
}
}
ans.sort((a, b) => a - b);
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.