The Numbers At Most N Given Digit Set problem gives you a sorted set of digits D and a number N. Count how many positive integers ≤ N can be formed using only digits from D (with repetition allowed). This hard coding problem uses digit DP combined with combinatorial prefix counting.
Microsoft, Amazon, and TikTok ask this because it requires combining digit-by-digit constraint checking with the counting of "all shorter numbers" using powers of |D|. The array, math, binary search, string, and dynamic programming interview pattern is the core.
Digit DP + prefix counting. Let N have L digits. Count in two parts: (1) all valid numbers with fewer than L digits: |D|^1 + |D|^2 + ... + |D|^(L-1). (2) Numbers with exactly L digits: digit-by-digit, for each position, count choices strictly less than N's digit, then multiply by |D|^(remaining positions). If exact match possible (digit is in D), continue; otherwise stop.
D=["1","3","5","7"], N=100. L=3. Part 1: |D|^1 + |D|^2 = 4+16 = 20 (1-digit and 2-digit numbers). Part 2: L=3 digit numbers.
"Count numbers with restricted digits ≤ N" always has two components: shorter numbers (|D|^1 + ... + |D|^(L-1)) and same-length numbers (digit-by-digit counting). The digit-by-digit pass checks: choices-less-than × |D|^remaining + (if digit in D: continue else stop). Practice this digit counting technique — it appears in many "count numbers satisfying property ≤ N" problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Super Egg Drop | Hard | Solve | |
| Number of Ways to Form a Target String Given a Dictionary | Hard | Solve | |
| Count of Integers | Hard | Solve | |
| Count the Number of Powerful Integers | Hard | Solve | |
| Delete Columns to Make Sorted III | Hard | Solve |