Iteration and recursion

2 ways to repeat Β· Part of Complexity and data

Key idea

Practice

7 questions Β· about 4 min

Explained step by step

Iteration

Iteration is repeating a block of code while a condition holds, and stopping when it no longer does. There are 2 common forms. The for loop fits when you know in advance how many rounds you'll do, like "add 1 to 100". The while loop checks the condition before each round and is more flexible: you can step by 2, or stop when something happens. It's like doing push-ups: "do 20" is a for; "do them until you're tired" is a while.

A loop can go inside another: for each round of the outer one, the inner one does all its rounds. If both go up to n, the total is n Γ— n = nΒ² operations. With n = 10 that's 100; with n = 1,000, 1 million. Each extra nested loop raises it one degree: 3 loops give nΒ³. It's like a times table: to build the one from 1 to 10 you do 10 rows of 10 calculations, 100 in total.

Recursion

Recursion is when a function solves itself by calling itself with a smaller problem. It has 2 phases. On the way down, it keeps calling itself with smaller and smaller numbers until it reaches a case it already knows how to solve, the base case. On the way back up, each call returns its result to the previous one, which combines it. It's like Russian dolls: you open one, inside there's a smaller one, until you reach the last; then you close them from the inside out.

Base case

The base case is the problem so small that the function solves it without calling itself again, and it's what makes recursion end. Without a base case, the function calls itself forever: f(3) calls f(2), f(1), f(0), f(βˆ’1)... until memory runs out. It's like going down a staircase: the base case is the floor. If there were no floor, you'd keep going down forever.

Call stack

Every time a function is called, the system reserves a spot in memory for its variables and for knowing where to return. That spot is called a frame, and frames pile up on the call stack. The frame is freed only when the call finishes. That's why f(1,000) has 1,000 frames at once when it reaches the base case, and recursion usually uses more memory than a loop. If too many pile up, the program fails with a stack overflow.

Tail recursion

In ordinary recursion, after the child call returns there's still work left: f(n) = n + f(n βˆ’ 1) has to add n on the way back, so its frame can't be dropped. In tail recursion, the recursive call is the last thing the function does: the running total is passed as a parameter, f(n, total) = f(n βˆ’ 1, total + n). Since nothing is pending, the compiler can reuse the same frame and use memory like a loop. Careful: Python, for example, doesn't do that optimization.

Recursion tree

When a recursive function calls itself more than once, the calls form a tree. In Fibonacci, f(n) = f(n βˆ’ 1) + f(n βˆ’ 2): each call branches into 2, those 2 into 4, and so on. With n levels, the tree can have close to 2ⁿ calls, and many repeat: f(3) is computed several times. Recursion fits naturally with problems that split into pieces, like trees, graphs, divide and conquer or backtracking.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

2 ways to repeat