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:
1fibonacci(0) = 0
2fibonacci(1) = 1
3fibonacci(2) = 1
4fibonacci(3) = 1 + 1 = 2
5fibonacci(4) = 1 + 2 = 3
6fibonacci(5) = 2 + 3 = 5So 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
nwill 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?
xxxxxxxxxx console.log('PASSED: ' + '`fibonacci(34)` should return `5702887`');var assert = require('assert');/* * @param {number} n The Fibonacci number to get. * @returns {number} The nth Fibonacci number */function fibonacci(num) { // fill this in return num;}try { assert.equal(fibonacci(1), 1); console.log('PASSED: ' + '`fibonacci(1)` should return `1`');} catch (err) { console.log(err);}try { assert.equal(fibonacci(17), 1597); console.log('PASSED: ' + '`fibonacci(17)` should return `1597`');} catch (err) { console.log(err);}try {Here's our guided, illustrated walk-through.
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.

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.
xxxxxxxxxxRoutine: f(n)Output: Fibonacci number at the nth placeBase case:1. if n==0 return 02. if n==1 return 1Recursive case:1. temp1 = f(n-1)2. temp2 = f(n-2)3. return temp1+temp2So we know we simply recursively call a method that returns the sum of the previous two fibonacci numbers.

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.

xxxxxxxxxxRoutine: fibonacciFastInput: nOutput: Fibonacci number at the nth placeIntermediate storage: n1, n2 to store f(n-1) and f(n-2) respectively1. if (n==0) return 02. if (n==1) return 13. n1 = 14. n2 = 05. for 2 .. n a. result = n1+n2 // gives f(n) b. n2 = n1 // set up f(n-2) for next number c. n1 = result // set up f(n-1) for next number6. return resultTo 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.
xxxxxxxxxxconst memoize = (callback) => { let memo = {}; return (args) => { if (memo[args]) { return memo[args]; } else { memo[args] = callback(args); return memo[args]; } };};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.
xxxxxxxxxxconst memoize = (callback) => { let memo = {}; return (args) => { if (memo[args]) { return memo[args]; } else { memo[args] = callback(args); return memo[args]; } };};function fibonacci(num) { if (num < 0) throw "num must be >= 0"; if (num === 0) return 0; if (num === 1) return 1; return fibonacci(num - 1) + fibonacci(num - 2);}const memoizedFib = memoize(fibonacci);const result = memoizedFib(30);console.log(result);One Pager Cheat Sheet
- Implement a function
fibonacci(n)which returns the number in theFibonaccisequence wheren <= 40, with a time and space complexity ofO(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
recursiveapproach, the Fibonacci Sequence can be calculated with a complexity of `O()$. - We can memo-ize (store) previous function calls and their results by using the
memoizefunction, which uses ahash mapas astorage. - To improve performance in recurring algorithms, use
memoizetowrapthefibonaccifunction.
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.
xxxxxxxxxx}var assert = require('assert');/* * @param {number} n The Fibonacci number to get. * @returns {number} The nth Fibonacci number */function fibonacci(num) { if (num < 0) throw 'num must be >= 0'; if (num === 0) return 0; if (num === 1) return 1; return fibonacci(num - 1) + fibonacci(num - 2);}try { assert.equal(fibonacci(1), 1); console.log('PASSED: ' + '`fibonacci(1)` should return `1`');} catch (err) { console.log(err);}try { assert.equal(fibonacci(17), 1597); console.log('PASSED: ' + '`fibonacci(17)` should return `1597`');} catch (err) { console.log(err);}try {Got more time? Let's keep going.
If you had any problems with this tutorial, check out the main forum thread here.


