DSA interview question
A party-planning app splits n guests into equal-sized tables and wants to show every possible table size, meaning all divisors of n in increasing order. Checking every number up to n is O(n), too slow for n near 10⁹. Divisors come in pairs (i, n / i) with i ≤ √n, so scanning i up to √n finds them all. Collect the small halves in order and the large halves in reverse, and take care not to list √n twice for perfect squares.
Example: 20 → 1 2 4 5 10 20.
vector<long long> divisors(long long n); // ascending; n >= 1
| Input | Output | Explanation |
|---|---|---|
20 | 1 2 4 5 10 20 | Pairs (1, 20), (2, 10), (4, 5) |
21191 | 1 21191 | A prime |
36 | 1 2 3 4 6 9 12 18 36 | 6 · 6 is listed once |
1 | 1 | |
12 | 1 2 3 4 6 12 |
(Confirm: n ≥ 1; each divisor appears once, in increasing 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