← Questions

Find the nth prime

0
NvidiaCodingLast asked Feb 2026

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 n 1-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: for n >= 6, p_n < n (ln n + ln ln n); a small fixed limit for n < 6.
  • Sieve details: start crossing out at i * i, only up to sqrt(limit), a byte array or bitset; O(L log log L) time and O(L) space for limit L.
  • What to do if the bound were too small (double and retry), and tests at small n and 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.
  • n up to 10^9: what changes (prime-counting methods), at least in outline?

Where it's asked

What it tests

math · primes · sieve

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