๐ŸŒ€ DSA

Recursion Explained โ€” Stop Fearing It in 10 Minutes

๐Ÿ“… Jun 28, 2026 โฑ 4 min read

Recursion = a function calling itself, each time with a smaller problem, until the problem is trivial. Two parts, always:

The anatomy

function factorial(n) {
  if (n <= 1) return 1;          // BASE CASE โ€” when to stop
  return n * factorial(n - 1);   // RECURSIVE CASE โ€” shrink the problem
}
// factorial(4) โ†’ 4 ร— 3 ร— 2 ร— 1 = 24

Forget the base case โ†’ infinite calls โ†’ "Maximum call stack size exceeded". That error IS recursion's failure mode.

Trace it (the skill interviews test)

factorial(3)
  = 3 * factorial(2)
        = 2 * factorial(1)
              = 1              โ† base case hit
        = 2 * 1 = 2
  = 3 * 2 = 6                  โ† stack unwinds

When recursion shines vs hurts

Practice: Fibonacci on our judge deliberately times out naive recursion at n=40 โ€” beat it with iteration.

โ† All Articles