PPractick

DSA interview question

Understanding Recursion: Print a Message N Times

Easy · Algorithms · Topics: recursion, basics

The problem

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.

Function signature

void printMessage(int n, const string& msg);  // prints msg n times, one per line

Test cases

Input (n, msg)OutputExplanation
(3, "Hi")Hi / Hi / HiThree calls print; the fourth hits the base case
(1, "Go")GoOne print, then the base case
(0, "Hi")(nothing)Base case immediately
(5, "Practice")Practice five timesMaximum recursion depth is 6 calls

(Confirm: n ≥ 0; each message is printed on its own line; no loops allowed.)

How interviewers phrase it

Clarifying questions to ask

  1. Can N be 0 or negative?
  2. How large can N be? (Very deep recursion can overflow the stack.)
  3. Is printing directly fine, or should the output be collected?

Follow-ups to expect

  1. "What happens without a base case?"
  2. "What's on the call stack?"
  3. "What's the time and space complexity?"
  4. "Is recursion slower than a loop?"
  5. "What's tail recursion?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in