The Nth Digit problem asks you to find the n-th digit in the infinite sequence 1, 2, 3, 4, ..., 9, 10, 11, 12, ... written consecutively as a string ("123456789101112..."). This Nth Digit coding problem requires range-based mathematical reasoning to efficiently skip ranges of same-length numbers.
Apple, Uber, Microsoft, Meta, Airbnb, Amazon, Google, and Bloomberg ask this because it tests the ability to decompose a large sequence into manageable ranges without iterating through each number. The math and binary search interview pattern is applied, with the key insight of grouping numbers by digit count.
Range mathematics. 1-digit numbers: 9 numbers × 1 digit = 9 digits. 2-digit numbers: 90 numbers × 2 digits = 180 digits. k-digit numbers: 9 × 10^(k-1) numbers × k digits. Subtract each range count from n until you find which range contains the n-th digit. Then find the exact number in that range and the digit within it.
n=15. Subtract 1-digit range: 9 digits. n=15-9=6 left. 2-digit range: each number has 2 digits. Number index = (6-1)/2 = 2, so the 3rd 2-digit number = 10+2=12. Digit within number = (6-1)%2 = 1 (0-indexed), so second digit of "12" = 2.
n=11: 9 digits used for 1-9. Remaining = 2. First 2-digit number = 10. Second digit of "10" = 0.
10^(k-1).Nth Digit belongs to the "skip-ranges" problem family where you work out how many digits/elements exist at each level and subtract. The formula: count of k-digit numbers = 9 × 10^(k-1), total digits in range = 9 × 10^(k-1) × k. Practice deriving this from first principles. After finding the correct range, the modular arithmetic for exact digit position is standard. This skip-range technique also appears in "kth symbol in grammar" and "digit counting" problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Reach a Number | Medium | Solve | |
| Minimum Garden Perimeter to Collect Enough Apples | Medium | Solve | |
| Minimum Time to Complete All Deliveries | Medium | Solve | |
| Arranging Coins | Easy | Solve | |
| Valid Perfect Square | Easy | Solve |