March 24
Recursion
What recursion means, when solving a problem by calling yourself makes sense, and why it freezes so many people in interviews.
What it is
Recursion means a function solves a problem by calling itself on a smaller or simpler version of the input.
It always needs two things:
- a base case
- a recursive step
When to use it
It appears a lot when the structure is naturally recursive:
- tree
- graph
- divide and conquer
- backtracking
Common mistake
The classic mistake is seeing recursion as a syntax trick.
The point is not “calling the function again.”
The point is recognizing that the problem repeats in similar subproblems.
Better question
Before using it, ask:
- what is the smallest case I can answer directly?
- how do I reduce the problem without breaking the logic?
- is each call actually moving toward termination?
Share this page
Copy the link manually from the field below.