The Fibonacci Sequence

I had my first technical interview a couple weeks ago. As the interview started, we introduced ourselves and immediately got into live coding. She asked me to write a code that would produce the nth number in the Fibonacci Sequence. I had recalled hearing those words at one point in my schooling. However, a refresher was definitely in order.

a series of numbers in which each number (Fibonacci number) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.

The way the code would work is to create an array where the first two elements are [0, 1]. The function that we write will take in the position of the element that we want to retrieve from the sequence. So if we want to return the 5th number in the Fibonacci Sequence it would return 3.

The way I went about doing this was using If/Else statements.The function would compare the element position it was given as an argument. If the position was less than or equal to 0 it would return an error. If the number was equal to 1 it would return 0, if it was equal to 2 it would return 1. For every other possibility it would return the previous number in the array plus the number before that so element -2 plus element -1. Using Iteration we could have the code run that sequence until the “nth” number in the sequence.

The code looked basically like this

const fibonacci = (num = 1) => {
   const series = [1, 1];
   for (let i = 2; i < num; i++) {
      const a = series[i - 1];
      const b = series[i - 2];
      series.push(a + b);
   };
   return series[num - 1];
};

After the technical interview ended, I had a better understanding of the idea. However the practical uses of the Fibonacci Sequence still remained a mystery to me. Googling it was helpful to a degree it stated that the Fibonacci Sequence is used practically in Stock market pricing as well as tracking bee and rabbit populations. How its used to do those things remains a foreign concept to me. Which only means further study is required.

All in all it was a fun teaching moment for me and another case of the constant reminder that I have ALOT to learn about the world of coding.

Leave a comment

Design a site like this with WordPress.com
Get started