DSA interview question
You're mentoring a junior engineer who keeps writing painfully slow recursive solutions that time out on moderately sized inputs. Rather than jumping straight to a specific problem, you decide to teach them the three-step framework every DP problem follows, using the simplest possible example — computing the nth Fibonacci number — to demonstrate the progression from naive recursion, to memoization (top-down DP), to tabulation (bottom-up DP), to fully space-optimized DP. Your task: implement all four stages for Fibonacci, and be ready to explain the why behind each transformation, since this same progression applies to virtually every DP problem you'll encounter.
Example: fib(5) = 5 (sequence: 0, 1, 1, 2, 3, 5).
int fibRecursive(int n); // Stage 1: naive recursion
int fibMemoization(int n, vector<int>& memo); // Stage 2: top-down DP
int fibTabulation(int n); // Stage 3: bottom-up DP
int fibSpaceOptimized(int n); // Stage 4: O(1) space DP
Input n | Output | Explanation |
|---|---|---|
| 0 | 0 | Base case |
| 1 | 1 | Base case |
| 2 | 1 | fib(1) + fib(0) = 1 + 0 |
| 5 | 5 | Sequence: 0,1,1,2,3,5 |
| 10 | 55 | Standard Fibonacci value |
| 40 | 102334155 | Demonstrates why naive recursion becomes infeasible (exponential blowup) at this size |
(Confirm: n is non-negative; this same 4-stage framework generalizes to any DP problem, not just Fibonacci.)
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