Skip to content
Veltoryx Technologies
FreeLesson 7.1Beginner2 min readUpdated 30 October 2026

The pattern: recursion

Master recursive problem solving, base case design, call stack mechanics, top-down memoization, and divide-and-conquer exponentiation.

A recursive function is a function that solves a problem by calling itself with smaller inputs. Every correct recursive algorithm relies on three fundamental pillars:

  1. Base Case: The simplest possible instance of the problem that can be answered immediately without further recursive calls.
  2. Recursive Step (Progress): Breaking the original problem into one or more strictly smaller subproblems that move towards the base case.
  3. Combination / Return: Trusting the recursive call to return the correct sub-result, then assembling it into the full solution.

01The core mental models

1. The Leap of Faith (Trust the Subproblem)

The biggest hurdle in recursion is trying to trace the entire call tree mentally. Instead, use mathematical induction:

  • Assume the recursive call solve(smaller_input) works 100% correctly.
  • Ask only: "Given the answer to the smaller problem, what single step must I take to form the answer for my current input?"
text
reverse(head):
    if head is null or head.next is null: return head
    newHead = reverse(head.next)   # Leap of faith: the rest is already reversed!
    head.next.next = head          # Only hook current node onto the reversed tail
    head.next = null
    return newHead

2. Top-Down Memoization (Eliminating Repeated Work)

Naive recursion with overlapping subproblems causes exponential branching (O(2^n)). Memoization caches previously computed results in a hash map or array:

text
fib(n):
    if n <= 1: return n
    if n in memo: return memo[n]
    memo[n] = fib(n - 1) + fib(n - 2)
    return memo[n]

By storing the result before returning, each subproblem state k is computed exactly once, collapsing time from O(2^n) to O(n).

Used for: Fibonacci Number, Climbing Stairs.

3. Divide and Conquer (Binary Halving)

Instead of shrinking the input by 1 (n - 1), divide the problem in half (n / 2).

text
fastPow(x, n):
    if n == 0: return 1.0
    half = fastPow(x, n / 2)
    return (n % 2 == 0) ? half * half : half * half * x

Computing half once and squaring it cuts the problem size in half at each step, reducing time complexity from O(n) to logarithmic O(log n).

Used for: Pow(x, n), K-th Symbol in Grammar.

4. Pointer Rewiring & Structural Recursion

Recursive traversal naturally processes linear data structures (linked lists, strings, trees) in reverse order during the unwinding phase of the call stack (post-order processing):

  • Pre-order work: performed before the recursive call (on the way down).
  • Post-order work: performed after the recursive call returns (on the way back up).

Used for: Reverse String, Reverse Linked List, Swap Nodes in Pairs, Search in BST.

02Recursion checklist

  1. Did you define the base case first? Missing base cases cause infinite recursion and StackOverflowError.
  2. Does every recursive call make strict progress toward the base case? Verify that arguments strictly decrease or converge.
  3. Are subproblems overlapping? If the same input state is reached multiple times, add memoization immediately.
  4. Is the call stack bounded? Standard JVM stack depth is around 10,000 frames. For linear recursion on n = 10^5, iterative loops are required in production.