Lesson 057

Recursion vs Iteration

Call Stack · Tail Calls · Conversion

1:00

What really separates recursion from iteration — call-stack cost, tail calls, TCO language support, and when to convert between the two.

By the end, you can

  • Explain the difference between the recursive and iterative models for the same algorithm.
  • Trace how the call stack grows and unwinds for a small recursive call (e.g. `factorial(5)`), counting the peak number of live frames.
  • State the space complexity of each approach and explain why an in-place loop is O(1) while recursion is O(depth).
  • Identify a tail call and rewrite a non-tail recursive function using an accumulator to make it tail-recursive.
  • List which languages/runtimes guarantee tail-call elimination and which do not (including the Python 3.14 myth).
  • Convert a recursive function to an iterative one using an explicit stack, and vice versa using an accumulator.
  • Choose between recursion and iteration given a problem description and constraints.
Up next in Recursion, Paradigms & Algorithm Analysis
Questions or feedback?