DSA interview question
A number-theory API receives hundreds of thousands of requests, each asking for the full prime factorisation of a number up to 10⁶ (for example 60 → 2 2 3 5). Trial division per request costs O(√n) each time. Precompute, once, the smallest prime factor (SPF) of every number with a sieve. Then factorising n is just repeatedly dividing by spf[n], which takes O(log n) steps, since each division at least halves n.
Example: 60 → 2 2 3 5; 97 → 97.
vector<int> buildSpf(int maxN); // spf[x] = smallest prime factor of x
vector<int> factorize(int n, const vector<int>& spf); // primes with multiplicity, ascending
| Input | Output | Explanation |
|---|---|---|
12 | 2 2 3 | |
60 | 2 2 3 5 | |
97 | 97 | Prime: spf[97] = 97 |
1 | (none) | |
1024 | 2 2 2 2 2 2 2 2 2 2 | 2¹⁰ |
999999 | 3 3 3 7 11 13 37 |
(Confirm: 1 ≤ n ≤ maxN (about 10⁶); factors are listed with multiplicity, in non-decreasing order.)
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