400. Nth Digit
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the nth digit in an infinitely long sequence formed by concatenating all positive integers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, .... The core challenge is to efficiently locate the number that contains the nth digit and then identify the digit itself without actually generating the sequence, as n can be very large.
Solution
Nth Digit in Python
class Solution:
def findNthDigit(self, n: int) -> int:
# Python's integers handle arbitrary size, so no overflow worries.
d = 1 # Number of digits
count = 9 # Count of numbers with d digits
start = 1 # Starting number with d digits
# 1. Find the number of digits `d` for the target number.
# This loop determines which block of numbers (e.g., 1-digit, 2-digit) the nth digit falls into.
while n > d * count:
n -= d * count
d += 1
count *= 10
start *= 10
# 2. Find the target number.
# After the loop, 'n' is the 1-based index of the digit within the current block.
# (n - 1) // d gives the 0-based index of the target number in its block.
num_index = (n - 1) // d
target_num = start + num_index
# 3. Find the specific digit.
# (n - 1) % d gives the 0-based index of the digit within the target number.
digit_index = (n - 1) % d
# Convert number to string and index it to get the digit.
return int(str(target_num)[digit_index])
Nth Digit in Java
class Solution {
public int findNthDigit(int n) {
// Use long to prevent overflow, as n is up to 2^31 - 1
// and intermediate calculations can exceed int's capacity.
long m = n;
long d = 1; // Number of digits
long count = 9; // Count of numbers with d digits
long start = 1; // Starting number with d digits
// 1. Find the number of digits `d` for the target number.
// This loop determines which block of numbers (e.g., 1-digit, 2-digit) the nth digit falls into.
while (m > d * count) {
m -= d * count;
d++;
count *= 10;
start *= 10;
}
// 2. Find the target number.
// After the loop, 'm' is the 1-based index of the digit within the current block.
// (m - 1) / d gives the 0-based index of the target number in its block.
long numIndex = (m - 1) / d;
long targetNum = start + numIndex;
// 3. Find the specific digit.
// (m - 1) % d gives the 0-based index of the digit within the target number.
long digitIndex = (m - 1) % d;
// Convert the number to a string to extract the digit at the calculated index.
String s = Long.toString(targetNum);
return s.charAt((int)digitIndex) - '0';
}
}
Nth Digit in C++
class Solution {
public:
int findNthDigit(int n) {
// Use long long to prevent overflow, as n is up to 2^31 - 1
// and intermediate calculations can exceed int's capacity.
long long m = n;
long long d = 1; // Number of digits
long long count = 9; // Count of numbers with d digits
long long start = 1; // Starting number with d digits
// 1. Find the number of digits `d` for the target number.
// This loop determines which block of numbers (e.g., 1-digit, 2-digit) the nth digit falls into.
while (m > d * count) {
m -= d * count;
d++;
count *= 10;
start *= 10;
}
// 2. Find the target number.
// After the loop, 'm' is the 1-based index of the digit within the current block.
// (m - 1) / d gives the 0-based index of the target number in its block.
long long num_index = (m - 1) / d;
long long target_num = start + num_index;
// 3. Find the specific digit.
// (m - 1) % d gives the 0-based index of the digit within the target number.
long long digit_index = (m - 1) % d;
// Convert the number to a string to extract the digit at the calculated index.
std::string s = std::to_string(target_num);
return s[digit_index] - '0';
}
};
Nth Digit in TypeScript
function findNthDigit(n: number): number {
// JavaScript's `number` type is a 64-bit float, which can safely represent
// integers up to 2^53 - 1. The max intermediate value (d * count) is ~8.1e9,
// which is well within this safe range. So, `number` is sufficient.
let m: number = n;
let d: number = 1; // Number of digits
let count: number = 9; // Count of numbers with d digits
let start: number = 1; // Starting number with d digits
// 1. Find the number of digits `d` for the target number.
// This loop determines which block of numbers (e.g., 1-digit, 2-digit) the nth digit falls into.
while (m > d * count) {
m -= d * count;
d++;
count *= 10;
start *= 10;
}
// 2. Find the target number.
// After the loop, 'm' is the 1-based index of the digit within the current block.
// (m - 1) / d gives the 0-based index of the target number in its block.
const numIndex = Math.floor((m - 1) / d);
const targetNum = start + numIndex;
// 3. Find the specific digit.
// (m - 1) % d gives the 0-based index of the digit within the target number.
const digitIndex = (m - 1) % d;
// Convert the number to a string to extract the digit at the calculated index.
const s = targetNum.toString();
return parseInt(s[digitIndex]);
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.