The Numbers With Same Consecutive Differences problem asks you to return all n-digit numbers where adjacent digits differ by exactly k. The numbers must not have leading zeros. This coding problem uses BFS or backtracking to build numbers digit by digit. The BFS and backtracking interview pattern is directly applied.
Flipkart, Amazon, and Google ask this to test BFS/DFS number generation with constraints. It validates that candidates can enumerate multi-digit numbers with digit-level constraints systematically without brute-forcing all n-digit numbers. The BFS and backtracking interview pattern is at the core.
BFS level-by-level expansion. Initialize queue with digits 1-9 (non-zero first digit). For each number in the queue: take its last digit d. If d+k ≤ 9, add the new number with digit d+k. If k > 0 and d-k ≥ 0, add the number with digit d-k. Expand until numbers have n digits. Special case: n=1 includes 0.
n=2, k=1. Start with 1-9.
d+k and d-k equal d — must avoid duplicates (add only once when k=0).BFS for generating numbers digit by digit is a clean pattern for "all n-digit numbers with property P" problems. The property (consecutive digit difference = k) constrains transitions. Handle k=0 carefully — adding both d+0 and d-0 would duplicate. Practice similar "generate all numbers with digit constraints" problems using BFS or DFS with pruning.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Brace Expansion | Medium | Solve | |
| Stepping Numbers | Medium | Solve | |
| Remove Invalid Parentheses | Hard | Solve | |
| All Paths From Source to Target | Medium | Solve | |
| Combinations | Medium | Solve |