Math for coding interviews/Recursion - functions that call themselves
Recursion Explained: Functions That Call Themselves
A recursive function solves a big problem by solving a slightly smaller version of the same problem, and keeps going until it hits a base case small enough to answer directly. Think of it as Russian nesting dolls - each doll contains a smaller version of itself, until you reach the tiny solid doll at the center.
See it for yourself
A vertical stack of boxes. Each box represents one function call. When the function calls itself, a new box gets stacked on top. When it returns, that box gets removed. The depth of the stack at any moment is how many nested calls are active.
Recursion - functions that call themselves
A recursive function solves a big problem by solving a slightly smaller version of the same problem, and keeps going until it hits a base case small enough to answer directly. Think of it as Russian nesting dolls - each doll contains a smaller version of itself, until you reach the tiny solid doll at the center.
Think of it this way: To find the total weight of a stack of boxes, you pick up the top box, weigh it, then ask someone to tell you the total weight of the remaining stack. They do the same thing. Eventually the last person just says "zero, there are no boxes left."
For algorithms: Trees, graphs, and divide-and-conquer algorithms are all inherently recursive. Once recursion clicks, these topics open up.
Real-world analogy
To find the total weight of a stack of boxes, you pick up the top box, weigh it, then ask someone to tell you the total weight of the remaining stack. They do the same thing. Eventually the last person just says "zero, there are no boxes left."
Why it matters in interviews
Trees, graphs, and divide-and-conquer algorithms are all inherently recursive. Once recursion clicks, these topics open up.