Coding questions · Pinterest
Implement scalable prime generator
This question evaluates a candidate's ability to implement scalable prime generation and related algorithmic optimization, including algorithm selection, time and memory complexity reasoning, edge-case handling, and verification via unit tests.
Write a function first_n_primes(n) that returns the first n prime numbers in ascending order. Constraints:
- 1 ≤ n ≤ 100,000.
- Aim for O(n log log n) expected time and near-linear memory.
- Avoid repeated trial division per candidate; use an efficient sieve and a tight upper bound estimate for the nth prime.
- Handle n=0 and n=1 edge cases cleanly.
- Include unit tests for n ∈ {0,1,5,10,100,100000} and verify the last prime for n=100000 equals the known value.
- Describe the complexity and key constants that dominate runtime for large n.
Implement Scalable Prime Generator
Write a function solution(n, return_last_only=False) that generates prime numbers efficiently using a sieve. By default, it should return the first n prime numbers in ascending order. To keep very large tests compact, if return_last_only is True, return a one-element list containing only the nth prime instead of the full list. For n = 0, return an empty list. Your implementation should avoid repeated trial division and should use a tight upper-bound estimate for the nth prime before sieving. For large inputs, the runtime is dominated by crossing off composite numbers in the sieve and, when returning the full answer, by materializing the output list.
Examples
Example 1
Input: (0, False)
Output: []
Explanation: There are no primes to return when n is 0.
Example 2
Input: (1, False)
Output: [2]
Explanation: The first prime number is 2.
Hints
- For n ≥ 6, the nth prime is less than about n(log n + log log n), which is a good starting bound for your sieve.
- You can cut memory roughly in half by storing only odd numbers in the sieve.