DSA interview question
Your team's code review bot flags any function it thinks might be too slow, but it can't explain why. You're asked to go through six small loop shapes that keep appearing in the codebase, work out the Big-O time complexity of each, and back your answers up by counting how many times the loop body actually runs. The goal is to be able to glance at any piece of code, name its complexity, and use the problem's constraints to decide whether it will run in time. That's a skill every interviewer checks after you write a solution.
Example: a loop that halves n each time, like for (i = n; i > 0; i /= 2), runs about log₂(n) times. For n = 1000 that's only 10 iterations.
long long countLinear(long long n); // for i < n -> O(n)
long long countNested(long long n); // for i < n, for j < n -> O(n^2)
long long countTriangular(long long n); // for i < n, for j < i -> O(n^2)
long long countHalving(long long n); // for i = n; i > 0; i /= 2 -> O(log n)
long long countSqrt(long long n); // for i = 1; i * i <= n; i++ -> O(sqrt n)
long long fibCalls(int n); // naive recursive Fibonacci -> O(2^n)
Input n | Linear | Nested | Triangular | Halving | Sqrt |
|---|---|---|---|---|---|
| 16 | 16 | 256 | 120 | 5 | 4 |
| 1000 | 1000 | 1000000 | 499500 | 10 | 31 |
Input n | fibCalls(n) | Explanation |
|---|---|---|
| 10 | 177 | Calls roughly double as n grows |
| 20 | 21891 | |
| 30 | 2692537 | About 2.7 million calls for n = 30 |
(Confirm: the functions return how many times the innermost statement runs, so the counts can be compared with the predicted Big-O.)
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