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 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find a specific digit in a sequence of numbers. The brute force way is to just write out all the numbers one after another until we reach the digit we are looking for. Then we can simply pick that digit out.
Here's how the algorithm would work step-by-step:
def find_nth_digit_brute_force(n):
digit_count = 0
number = 1
while True:
number_string = str(number)
number_length = len(number_string)
# Update total digit count
digit_count += number_length
# Check if we have passed the nth digit
if digit_count >= n:
# Calculate the index of the target digit
index_of_digit = number_length - (digit_count - n) - 1
# Return the target digit
return int(number_string[index_of_digit])
number += 1The goal is to find a single digit within a sequence of increasing numbers written out one after another. Instead of building the entire sequence, we determine which number contains the nth digit, and then extract that digit.
Here's how the algorithm would work step-by-step:
def find_nth_digit(n):
digit_length = 1
count_of_numbers = 9
while n > digit_length * count_of_numbers:
n -= digit_length * count_of_numbers
digit_length += 1
count_of_numbers *= 10
# Determine which number contains the nth digit.
starting_number = 10 ** (digit_length - 1)
number_index = (n - 1) // digit_length
target_number = starting_number + number_index
# Calculate the index of the digit within the number.
digit_index = (n - 1) % digit_length
target_number_string = str(target_number)
# Extract the correct digit from the number.
return int(target_number_string[digit_index])| Case | How to Handle |
|---|---|
| n = 0 | Since n is a positive integer, treat n=0 as an invalid input and throw an exception or return -1. |
| n = 1 | Return 1 directly, as the first digit is '1'. |
| n is a large number, close to Integer.MAX_VALUE, causing potential integer overflow during calculations of digit count. | Use long data type to store intermediate calculations, such as the number of digits and the base value to prevent integer overflow. |
| n is within the range of single-digit numbers (1-9) | Directly return n as an integer, since it represents the nth single digit number. |
| n is within the range of double-digit numbers (10-99) | Calculate the offset within the double-digit range and extract the corresponding digit. |
| n is very large such that efficient calculation becomes crucial | Optimize the digit counting loop to quickly skip over ranges of numbers (1-digit, 2-digit, 3-digit, etc.) before narrowing down to the target number. |
| n such that resulting number has leading zeros | Leading zeros are not applicable to this problem because we are dealing with the sequence of positive integers, which don't have leading zeros by definition. |
| No valid solution exists (theoretically impossible for positive n) | Since 'n' is defined as a positive integer, a valid solution should always exist, but a check for n<1 could be included as a defensive measure, though not strictly necessary, and handled by returning an error value or exception. |