DSA interview question
A cryptography teaching tool lets students type a number and shows its distinct prime factors in increasing order, for example 100 → 2, 5. Numbers go up to about 10⁹, so testing every candidate up to n is too slow. Trial division up to √n is enough: divide out each factor completely when you find it (which guarantees every factor found is prime), and whatever is left above 1 at the end must itself be a prime larger than √n.
Example: 100 → 2 5; 13 → 13.
vector<long long> primeFactors(long long n); // distinct primes, ascending; n >= 1
| Input | Output | Explanation |
|---|---|---|
100 | 2 5 | 100 = 2² · 5² |
35 | 5 7 | |
13 | 13 | A prime is its own only prime factor |
1 | (none) | 1 has no prime factors |
30030 | 2 3 5 7 11 13 | The product of the first six primes |
1000000007 | 1000000007 | A large prime: the loop stops at √n |
(Confirm: each prime is listed once, in increasing order; n ≤ about 10¹².)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in