Skip to article content
FrontendXLesson 12 / 24

Recursion and the call stack

Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.

intermediate · about 35 minutes · 3 exercises

  • Predict
  • Explain
  • Implement

What you will be able to do

#
  • Write a recursive function with a correct base case and recursive case.
  • Trace recursion using the call stack.
  • Explain why recursion depth consumes O(depth) space.

Overview

#

A function that calls itself

Recursion solves a problem by solving a smaller version of the same problem. Every recursive function needs a base case that stops the recursion and a recursive case that moves toward the base case.

snippet.txt
function factorial(n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

The call stack

Each call pauses and waits for the inner call to return. The runtime keeps these paused frames on the call stack:

snippet.txt
factorial(4)
  factorial(3)
    factorial(2)
      factorial(1) -> 1
    -> 2
  -> 6
-> 24

The depth of the stack equals the number of pending calls, which is O(n) here. That space is real: deep recursion can overflow the stack.

Recursion is a stack in disguise

Any recursion can be rewritten with an explicit stack. Recursion is often clearer for trees and divide-and-conquer, while an explicit stack avoids depth limits and can be easier to reason about for wide searches.

Divide and conquer

A recursive function that splits the input in half and combines the results is divide and conquer. Merge sort and binary search are examples. The recurrence T(n) = 2T(n/2) + O(n) solves to O(n log n), which you can justify by counting the levels of the recursion tree and the work per level.

Practice next

  • Convert factorial to an iterative loop and compare space.
  • Trace a recursive tree traversal and write the visit order.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What is the maximum call-stack depth during factorial(5)?

    starter.javascript
    function factorial(n) {
      if (n <= 1) return 1;
      return n * factorial(n - 1);
    }

    Reveal the reference answer
    reference.javascript
    The calls are factorial(5), (4), (3), (2), (1). That is five frames on the stack at the deepest point.

    Authored expected output: 5

    Depth equals the input value because each call decreases n by one.

  2. Exercise 2Implement

    Write a recursive function that returns the sum of the first n positive integers.

    starter.javascript
    function sumTo(n) {
      // your code here
    }

    Reveal the reference answer
    reference.javascript
    function sumTo(n) {
      if (n <= 0) return 0;
      return n + sumTo(n - 1);
    }

    Authored expected output: 10 for sumTo(4)

    The base case returns 0 at n = 0, and each frame adds its own n.

  3. Exercise 3Explain

    Explain why a recursive algorithm that only recurses once still uses O(n) space in the worst case.

    Reveal the reference answer
    reference.text
    Each pending call keeps a frame on the call stack until its inner call returns. If the recursion goes n levels deep before hitting the base case, n frames are alive at once, so the stack uses O(n) space even though only one branch is active.

    Authored expected output: n nested pending frames are alive at the deepest point, so O(n) stack space.

    Time and space can differ; a linear-time recursion can still use linear stack space.

Common misconceptions

#
  • Recursion uses no extra memory. Each pending call consumes stack space, so depth costs memory.
  • Every recursive function must have two branches. A function that recurses once still needs a base case.
  • Recursion is always slower than a loop. The overhead is real, but the asymptotic cost is often identical.

Follow-up practice

#
  • dsa-from-zero-trees-and-traversal
  • dsa-from-zero-dynamic-programming
  • dsa-from-zero-backtracking

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.