Find the nth prime number
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question assesses algorithmic problem-solving and number-theoretic computation skills, requiring efficient prime generation and complexity analysis in the Coding & Algorithms domain.
Constraints
- 1 <= n <= 100000
- The answer fits in a 32-bit signed integer for the given constraints
- An efficient solution is expected, such as using the Sieve of Eratosthenes with a suitable upper bound
Examples
Input: 1
Expected Output: 2
Explanation: The 1st prime number is 2.
Input: 2
Expected Output: 3
Explanation: The sequence of primes starts as 2, 3, 5, 7, ...
Input: 6
Expected Output: 13
Explanation: The first six primes are 2, 3, 5, 7, 11, 13.
Input: 10
Expected Output: 29
Explanation: The 10th prime number is 29.
Input: 100
Expected Output: 541
Explanation: Counting primes in order, the 100th prime is 541.
Input: 1000
Expected Output: 7919
Explanation: The 1000th prime number is 7919.
Hints
- Instead of testing each number individually for primality, mark multiples of known primes using a sieve.
- To size the sieve, use an upper bound for the n-th prime: for n >= 6, p_n < n * (log n + log log n).