Programming6 min read10 interview questions
Recursion explained
A function that solves a problem by solving a smaller copy of it.
In one sentenceRecursion means a function calls itself with a smaller version of the problem until it reaches one so small the answer is obvious.
Think of it like thisIt is opening a set of nesting dolls: open one, find a smaller one, repeat until you reach the tiny solid one, then put them back together.
The big picture
Tap any step to see where to use it, how to use it and an example result.
The two parts
Every recursive function needs a base case, which is the simplest situation with a direct answer, and a recursive case, which reduces the problem and calls itself. Without a base case it never stops.
A small example
To add up numbers from 1 to n: if n is 0, return 0. Otherwise return n plus the sum up to n minus 1. Each call waits for the smaller one, and the answers add up as the calls return.
When to use it
It suits things that contain smaller versions of themselves, such as folders inside folders, family trees and some sorting methods. For simple counting, a loop is often clearer and uses less memory.
Common mistakes
- Forgetting the base case, which causes a stack overflow.
- Not making the problem smaller on each call.
- Recalculating the same values many times, which can be fixed by remembering the results.
Quick recap
- A base case stops it
- Each call must make the problem smaller
- It fits nested structures
- A loop is sometimes simpler
Try it yourself
Write a function that prints every file in a folder and all its subfolders, using recursion.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is recursion?Basic
When a function solves a problem by calling itself on a smaller version of the same problem, until it reaches a case simple enough to answer directly.
What is a base case and why is it essential?Basic
The simplest case that stops the recursion. Without it the function would call itself forever and crash with a stack overflow.
What is the recursive case?Basic
The part where the function calls itself with a smaller or simpler input, moving closer to the base case.
Give a simple example of recursion.Basic
Factorial: n! equals n times (n-1)!, and 0! equals 1. The function calls itself with n-1 until it reaches 0.
What is the call stack and what is a stack overflow?Intermediate
The call stack records active function calls. Each recursive call adds a frame, and if recursion goes too deep, the stack runs out of space and the program crashes with a stack overflow.
Recursion versus iteration: how do you choose?Intermediate
Iteration uses loops and is usually faster and uses less memory. Recursion is often clearer for trees, nested data and divide-and-conquer problems. Any recursion can be rewritten as a loop with an explicit stack.
Why is naive recursive Fibonacci so slow?Intermediate
It recalculates the same values many times, giving exponential time. Storing results (memoization) or using a loop brings it down to linear time.
What is memoization?Intermediate
Saving the result of a function for given inputs so repeated calls return the stored answer instead of recomputing it.
What is tail recursion?Advanced
When the recursive call is the last thing a function does. Some languages can reuse the same stack frame for it, avoiding overflow, but not all do; Python and JavaScript engines generally do not.
What problems suit recursion, and how does it relate to dynamic programming?Advanced
Trees, graphs, file systems, parsing, backtracking and divide-and-conquer. Dynamic programming is recursion with overlapping subproblems stored so each is solved only once.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.