Mark As Completed Discussion

Good morning! Here's our prompt for today.

Implement a function that returns the Fibonnaci number for a given integer input. In a Fibonacci sequence, each number is the sum of the two preceding ones.

The simplest is the series: 1, 1, 2, 3, 5, 8, etc.

This is because:

SNIPPET
1fibonacci(0) = 0
2fibonacci(1) = 1
3fibonacci(2) = 1
4fibonacci(3) = 1 + 1 = 2
5fibonacci(4) = 1 + 2 = 3
6fibonacci(5) = 2 + 3 = 5

So if we were to invoke fibonacci(5), we'd want to return 5 (because the second to last number was 2 + last number was 3).

Constraints

  • The value of n will always be a non negative integer and <=40
  • The answer is guaranteed to fit in the integer data type
  • Expected time and space complexity are both O(n)

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's how we would solve this problem...

How do I use this guide?

Recall the definition of the Fibonacci numbers: they are a sequence of integers in which every number after the first two (0 and 1) is the sum of the two preceding numbers. To arrive at the sequence, we can quickly recognize there's a pattern in place, where we are repeating a similar calculation multiple times with different numbers.

Step Two

Time to reach into our toolbox-- this can readily be solved with recursion. At each recursive call, run the same calculation based off the same function for the prior elements. You can see the pseudo-code provided for how this can be accomplished.

TEXT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

So we know we simply recursively call a method that returns the sum of the previous two fibonacci numbers.

Memoization

If we look closely at the recursive tree, we can see that the function is computed twice for f(3), thrice for f(2) and many times for the base cases f(1) and f(0). The overall complexity of this pseudo-code is therefore exponential O($2^n$). We can very well see how we can achieve massive speedups by storing the intermediate results and using them when needed.

Memoization
TEXT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

To memo-ize previous function calls, we can use a nifty memoize function.

What we're doing in memoize is utilizing a hash map (in this case, a plain old JS object) to store previous calculations. The key will be the arguments called-- for example, 5 in fib(5), and the value attached will be the result of the callback. In this example, we utilize the closure pattern so that ...args are the parameters of the callback function, and not of memoize.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Now we can wrap our fibonacci function with the memoize function for a more efficient solution. During an interview, it's best to bring up the use of this pattern whenever there's several repeating subproblems.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

One Pager Cheat Sheet

  • Implement a function fibonacci(n) which returns the number in the Fibonacci sequence where n <= 40, with a time and space complexity of O(n).
  • The Fibonacci sequence can be effectively generated using Recursion in order to efficiently calculate each successive number from the sum of the two prior.
  • By storing intermediate results and using a recursive approach, the Fibonacci Sequence can be calculated with a complexity of `O(2n)$.
  • We can memo-ize (store) previous function calls and their results by using the memoize function, which uses a hash map as a storage.
  • To improve performance in recurring algorithms, use memoize to wrap the fibonacci function.

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Great job getting through this. Let's move on.

If you had any problems with this tutorial, check out the main forum thread here.