DSA interview question
A scheduling tool splits N minutes of workshop time into equal-length sessions, and the organiser wants to see every possible session length, meaning every divisor of N, in increasing order. N can be as large as 10^9, so checking every number from 1 to N would take seconds per request. The key observation is that divisors come in pairs (i, N / i), and one of each pair is always at most √N. Checking only up to √N finds all of them.
Example: N = 36 → 1 2 3 4 6 9 12 18 36.
vector<long long> divisors(long long n); // all divisors of n in increasing order, n >= 1
Input n | Output | Explanation |
|---|---|---|
| 36 | 1 2 3 4 6 9 12 18 36 | 6 × 6 = 36, so 6 appears only once |
| 20 | 1 2 4 5 10 20 | Pairs (1, 20), (2, 10), (4, 5) |
| 1 | 1 | |
| 13 | 1 13 | A prime has exactly two divisors |
| 100 | 1 2 4 5 10 20 25 50 100 | Perfect square: 10 once |
| 999999937 | 1 999999937 | Largest prime below 10^9; only about 31,623 checks |
(Confirm: 1 ≤ n ≤ 10^9 (or larger); output in increasing order without duplicates.)
i <= n / i instead of i * i <= n?"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