DSA interview question
A rocket-launch countdown screen shows N, N−1, ..., 1, and the exercise is to produce it recursively. This is the mirror image of the previous problem: with only n as a parameter, printing before the recursive call gives a countdown naturally, while carrying an index that counts up and printing after the recursive call produces the same countdown through backtracking. Solving both directions with both techniques makes the "work before vs after the call" idea stick, and that idea underpins preorder vs postorder traversal later.
Example: N = 5 → 5 4 3 2 1.
void printDown(int n); // print n, then recurse on n - 1
void printDownByBacktracking(int i, int n); // recurse on i + 1 first, then print i (call with i = 1)
Input n | Output | Explanation |
|---|---|---|
| 5 | 5 4 3 2 1 | Both versions |
| 1 | 1 | |
| 0 | (nothing) | |
| 3 | 3 2 1 |
(Confirm: n ≥ 0; numbers separated by spaces; 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