PPractick

DSA interview question

Print Name N Times Using Recursion

Easy · Algorithms · Topics: recursion, basics

The problem

A greeting-card printer must print the recipient's name N times down the card, and the exercise is to do it recursively. There are two natural ways to write it. The first counts down from N to 0, as in the previous problem. The second uses a parameterised recursion that counts up from 1 to N, carrying the current position as an extra argument. Being comfortable with both, and knowing which parameter carries the progress, is the stepping stone to the "Print 1 to N" and "Print N to 1" problems and later to recursive tree traversals.

Example: printName("Asha", 3) prints Asha three times.

Function signature

void printNameCountdown(const string& name, int n);    // recursion on n: n, n-1, ..., 1
void printNameCountUp(const string& name, int i, int n); // recursion on i: 1, 2, ..., n

Test cases

InputOutputExplanation
printNameCountdown("Asha", 3)Asha ×3Stops when n reaches 0
printNameCountUp("Asha", 1, 3)Asha ×3Stops when i exceeds n
printNameCountdown("Ravi", 1)Ravi ×1
printNameCountdown("Ravi", 0)(nothing)Base case immediately

(Confirm: n ≥ 0; each name on its own line; no loops.)

How interviewers phrase it

Clarifying questions to ask

  1. Can N be 0?
  2. How large can N be? (Stack depth.)
  3. Should it print directly or return a list?

Follow-ups to expect

  1. "What's the difference between your two versions?"
  2. "Which parameter guarantees termination?"
  3. "What's parameterised vs functional recursion?"
  4. "Complexity?"

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