What is recursion, and how does it work in programming?

Quick Overview

Explain what recursion is and what happens when a function calls itself in a running program. Tests whether a candidate can trace recursive calls on the call stack, reason about stack depth and repeated work, and decide when iteration is the better choice.

What is recursion, and how does it work in programming?

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

What is recursion, and how does it work in programming? Explain what actually happens at runtime when a function calls itself. ```hint Follow the calls Trace a tiny recursive function by hand and write down what is waiting on the call stack at each step. Then ask what stops the calls from going on forever. ``` ### What a Strong Answer Covers - A definition that names the two parts every recursive function needs and how they relate - What happens on the call stack as calls are made and as they return - A small example traced step by step - The costs and limits of recursion, such as stack depth and repeated work - When recursion is a good fit and how to turn it into iteration when it is not ### Follow-up Questions - What is tail recursion, and does Python optimize it? - Why is the naive recursive Fibonacci function slow, and how do you fix it? - How would you rewrite a recursive tree traversal without recursion? - What is Python's default recursion limit, and what happens when a program exceeds it?

Overview: Explain what recursion is and what happens when a function calls itself in a running program. Tests whether a candidate can trace recursive calls on the call stack, reason about stack depth and repeated work, and decide when iteration is the better choice.

PracHub logo
General
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

What is recursion, and how does it work in programming? Explain what actually happens at runtime when a function calls itself.

What a Strong Answer Covers Guidance

  • A definition that names the two parts every recursive function needs and how they relate
  • What happens on the call stack as calls are made and as they return
  • A small example traced step by step
  • The costs and limits of recursion, such as stack depth and repeated work
  • When recursion is a good fit and how to turn it into iteration when it is not

Follow-up Questions Guidance

  • What is tail recursion, and does Python optimize it?
  • Why is the naive recursive Fibonacci function slow, and how do you fix it?
  • How would you rewrite a recursive tree traversal without recursion?
  • What is Python's default recursion limit, and what happens when a program exceeds it?