DSA interview question
Before recursion shows up in trees, backtracking and dynamic programming, you need a rock-solid picture of what actually happens when a function calls itself. The simplest possible example is printing a message N times without a loop. A function prints the message once and then asks a smaller copy of itself to print it N − 1 more times. The exercise is less about the output and more about being able to explain the three parts every recursive function needs: a base case, a recursive case, and progress toward the base case. It's also a chance to see the call stack, and what happens when the base case is missing.
Example: printMessage(3, "Hi") prints Hi three times.
void printMessage(int n, const string& msg); // prints msg n times, one per line
Input (n, msg) | Output | Explanation |
|---|---|---|
| (3, "Hi") | Hi / Hi / Hi | Three calls print; the fourth hits the base case |
| (1, "Go") | Go | One print, then the base case |
| (0, "Hi") | (nothing) | Base case immediately |
| (5, "Practice") | Practice five times | Maximum recursion depth is 6 calls |
(Confirm: n ≥ 0; each message is printed on its own line; no loops allowed.)
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