← Questions
Find the nth prime
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Nvidia's technical phone screen, last asked Feb 2026. It tests math, primes and sieve.
Given n (1-indexed), return the nth prime number. A trial-division check of every candidate is too slow for the larger values of n, so the solution should generate primes in bulk.
Interface
Unless you agree on a different interface with the interviewer, assume:
nth_prime(n: int) -> int
Example
nth_prime(1) -> 2
nth_prime(2) -> 3
nth_prime(3) -> 5
nth_prime(6) -> 13
nth_prime(100) -> 541
nth_prime(10000) -> 104729
nth_prime(100000) -> 1299709
Constraints
-
1 <= n <= 100000; the answer fits in a 32-bit signed integer.
Clarifying questions
- Is
n1-indexed? - What is the largest
n, and will there be many queries? - Is memory limited?
What a strong answer covers
- The baseline: test each odd candidate by trial division up to its square root, and its cost.
- A sieve of Eratosthenes sized by an upper bound on the
nth prime: forn >= 6,p_n < n (ln n + ln ln n); a small fixed limit forn < 6. - Sieve details: start crossing out at
i * i, only up tosqrt(limit), a byte array or bitset;O(L log log L)time andO(L)space for limitL. - What to do if the bound were too small (double and retry), and tests at small
nand at the bound edge cases.
Follow-ups
- Answer many queries: precompute once and index.
- Memory is tight: use a segmented sieve, or sieve odd numbers only.
-
nup to 10^9: what changes (prime-counting methods), at least in outline?
Where it's asked
- Nvidia · Technical phone screen · Last asked Feb 2026Live technical interview, 45 to 90 minutes; a project discussion, then a small task
What it tests
math · primes · sieve
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
