Skip to main content

Recursion

What recursion means, when solving a problem by calling yourself makes sense, and why it freezes so many people in interviews.

Andrews Ribeiro

Andrews Ribeiro

Founder & Engineer

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:

  1. what is the smallest case I can answer directly?
  2. how do I reduce the problem without breaking the logic?
  3. is each call actually moving toward termination?

Keep exploring