DSA interview question
The Fibonacci sequence, 0, 1, 1, 2, 3, 5, 8, ..., where each number is the sum of the two before it, is the standard first example of multiple recursion: a function that calls itself more than once. Written directly from the definition, it's beautifully short and catastrophically slow, because the same values get recomputed again and again. The exercise is to write the direct recursion, see exactly why it explodes (by counting calls), and then fix it with memoisation and with a simple loop.
Example: fib(10) = 55; fib(50) = 12586269025.
long long fibRecursive(int n); // direct recursion: exponential
long long fibMemo(int n); // memoised recursion: linear
long long fibIterative(int n); // two variables: linear time, constant space
Input n | Output | Explanation |
|---|---|---|
| 0 | 0 | Base case |
| 1 | 1 | Base case |
| 2 | 1 | 1 + 0 |
| 10 | 55 | |
| 30 | 832040 | Direct recursion makes about 2.7 million calls here |
| 50 | 12586269025 | Exceeds int; direct recursion would take minutes |
(Confirm: fib(0) = 0, fib(1) = 1; 0 ≤ n ≤ 90 so the result fits in a long long.)
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