Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...].
Example 1:
Input: n = 3 Output: 3
Example 2:
Input: n = 11 Output: 0 Explanation: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
Constraints:
1 <= n <= 231 - 1Problem summary: Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Binary Search
3
11
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #400: Nth Digit
class Solution {
public int findNthDigit(int n) {
int k = 1, cnt = 9;
while ((long) k * cnt < n) {
n -= k * cnt;
++k;
cnt *= 10;
}
int num = (int) Math.pow(10, k - 1) + (n - 1) / k;
int idx = (n - 1) % k;
return String.valueOf(num).charAt(idx) - '0';
}
}
// Accepted solution for LeetCode #400: Nth Digit
func findNthDigit(n int) int {
k, cnt := 1, 9
for k*cnt < n {
n -= k * cnt
k++
cnt *= 10
}
num := int(math.Pow10(k-1)) + (n-1)/k
idx := (n - 1) % k
return int(strconv.Itoa(num)[idx] - '0')
}
# Accepted solution for LeetCode #400: Nth Digit
class Solution:
def findNthDigit(self, n: int) -> int:
k, cnt = 1, 9
while k * cnt < n:
n -= k * cnt
k += 1
cnt *= 10
num = 10 ** (k - 1) + (n - 1) // k
idx = (n - 1) % k
return int(str(num)[idx])
// Accepted solution for LeetCode #400: Nth Digit
struct Solution;
impl Solution {
fn find_nth_digit(n: i32) -> i32 {
let mut size = 1;
let mut start = 1;
let mut count = 9;
let mut n = n as usize;
while n > size * count {
n -= size * count;
size += 1;
start *= 10;
count *= 10;
}
let x = start + (n - 1) / size;
let v: Vec<i32> = format!("{}", x)
.bytes()
.map(|b| (b - b'0') as i32)
.collect();
v[(n - 1) % size]
}
}
#[test]
fn test() {
let n = 3;
let res = 3;
assert_eq!(Solution::find_nth_digit(n), res);
let n = 11;
let res = 0;
assert_eq!(Solution::find_nth_digit(n), res);
}
// Accepted solution for LeetCode #400: Nth Digit
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #400: Nth Digit
// class Solution {
// public int findNthDigit(int n) {
// int k = 1, cnt = 9;
// while ((long) k * cnt < n) {
// n -= k * cnt;
// ++k;
// cnt *= 10;
// }
// int num = (int) Math.pow(10, k - 1) + (n - 1) / k;
// int idx = (n - 1) % k;
// return String.valueOf(num).charAt(idx) - '0';
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.