Can you print numbers from 1 to N without using loops?

Coding
Medium
Infosys
61K views

This challenge requires alternative control flow mechanisms like recursion. It tests recursion understanding and stack management.

Why Interviewers Ask This

Interviewers ask this to test creativity and deep understanding of recursion. It checks if candidates can replace iterative constructs with recursive calls and manage the base case correctly. It also highlights awareness of stack overflow risks.

How to Answer This Question

Explain using a recursive function that prints the current number and calls itself with the next number. Define a base case to stop recursion when the number exceeds N. Discuss the call stack depth and potential limitations.

Key Points to Cover

  • Recursive function definition
  • Base case implementation
  • Incremental call
  • Stack overflow risk

Sample Answer

To print numbers from 1 to N without loops, I can use recursion. I define a function that takes the current number and N. Inside the function, I print the current number and then call the function again with the current…

Common Mistakes to Avoid

  • Missing base case
  • Infinite recursion
  • Incorrect increment logic

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 80 Coding questionsBrowse all 149 Infosys questions