</>CodexBash
All articles
Programming

Recursion vs iteration

When to solve a problem with recursion and when with loops: how recursion works with base cases and the call stack, the same problems solved both ways, stack depth limits, tail calls, memoisation, converting recursion to iteration with an explicit stack, and how to choose in interviews and real code.

CodexBash, , 5 min read

Every repetitive computation can be written two ways: with a loop that repeats steps while updating variables, or with recursion, where a function solves a problem by calling itself on a smaller version of the same problem. Beginners often find recursion mysterious and interviewers love it, because trees, graphs, backtracking and divide-and-conquer algorithms are naturally recursive. Yet recursion has real costs that loops avoid. This article explains how recursion works, compares the two on the same problems, and gives clear rules for choosing.

Sign in to read the rest

The whole 5 minute article is free with an account, along with every problem, lesson and tutorial. No card.