PPractick

DSA interview question

Print N to 1 Using Recursion

Easy · Algorithms · Topics: recursion, basics

The problem

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.

Function signature

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)

Test cases

Input nOutputExplanation
55 4 3 2 1Both versions
11
0(nothing)
33 2 1

(Confirm: n ≥ 0; numbers separated by spaces; no loops.)

How interviewers phrase it

Clarifying questions to ask

  1. Is an extra parameter allowed?
  2. Range of N?
  3. Separator?

Follow-ups to expect

  1. "How does the backtracking version print in reverse when i counts up?"
  2. "Which of your functions is tail recursive?"
  3. "How does this relate to tree traversals?"
  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