Grokking Dynamic Programming Patterns for Coding Interviews
Vote

0% completed

Solution: Fibonacci numbers

Problem Statement

Why this is a Fibonacci Numbers problem

Basic Solution

Top-down Dynamic Programming with Memoization

Bottom-up Dynamic Programming

Memory optimization

Problem Statement

Write a function to calculate the nth Fibonacci number.

Fibonacci numbers are a series of numbers in which each number is the sum of the two preceding numbers. First few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, ...

Mathematically we can define the Fibonacci numbers as:

Fib(n) = Fib(n-1) + Fib(n-2), for n > 1 Given that: Fib(0) = 0, and Fib(1) = 1

Constraints:

  • 0 <= n <= 30

Why this is a Fibonacci Numbers problem

What the question saysThe signal it matches
"each number is the sum of the two preceding numbers"the answer at position i depends on a fixed number of earlier positions
"Fib(n) = Fib(n-1) + Fib(n-2), for n > 1"no choice reaches more than a couple of steps back

This is the count the ways to arrive variant: one state is the value at position i.

The closest alternative. The plain recursion the question hands you. It is correct, it matches the definition line for line, and it recomputes the same values again and again.

Count the waste, because it is the reason this chapter exists. Computing Fib(30) by plain recursion makes about 2.7 million calls, and Fib(28) alone is recomputed thousands of times. Storing each answer the first time reduces that to 31 computed values. The introduction calls this the tell for the whole pattern: a recursion that is correct but recomputes. Only two earlier values are ever needed, so the table can shrink to two variables.

Basic Solution

A basic solution could be to have a recursive implementation of the mathematical formula discussed above:

Python3
Python3

. . . .

The time complexity of the above algorithm is exponential O(2^n) as we are making two recursive calls in the same function. The space complexity is O(n) which is used to store the recursion stack.

Let's visually draw the recursion for CalculateFibonacci(4) to see the overlapping subproblems:

Recursion tree for calculating Fibonacci numbers
Recursion tree for calculating Fibonacci numbers

We can clearly see the overlapping subproblem pattern: fib(2) has been called twice and fib(1) has been called thrice. We can optimize this using memoization to store the results for subproblems.

Top-down Dynamic Programming with Memoization

We can use an array to store the already solved subproblems. Here is the code:

Python3
Python3

. . . .

Bottom-up Dynamic Programming

Let's try to populate our dp[] array from the above solution, working in a bottom-up fashion. Since every Fibonacci number is the sum of the previous two numbers, we can use this fact to populate our array.

Here is the code for the bottom-up dynamic programming approach:

Python3
Python3

. . . .

The above solution has time and space complexity of O(n).

Memory optimization

We can optimize the space used in our previous solution. We don't need to store all the Fibonacci numbers up to 'n', as we only need two previous numbers to calculate the next Fibonacci number. We can use this fact to further improve our solution:

Python3
Python3

. . . .

The above solution has a time complexity of O(n) but a constant space complexity O(1).

Reading Progress

0%


Vote for new content

On This Page

Problem Statement

Why this is a Fibonacci Numbers problem

Basic Solution

Top-down Dynamic Programming with Memoization

Bottom-up Dynamic Programming

Memory optimization