DSA interview question
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.
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
| Input | Output | Explanation |
|---|---|---|
printNameCountdown("Asha", 3) | Asha ×3 | Stops when n reaches 0 |
printNameCountUp("Asha", 1, 3) | Asha ×3 | Stops 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.)
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