TABLE OF CONTENTS

The Recursion That Cost Me a Full Stack Position

Author Sagi Liba
Sagi Liba on Jul 4, 2020
4 min šŸ•

Itā€™s 6 PM. Iā€™m tired and after a long day at work. Iā€™ve arrived at the interview site and met the team leader whom I will be working with. we start with the basic ā€œTell me about yourself questionsā€, and go straight to the JavaScript coding test.

Iā€™ve been handed a piece of paper, 30 minutes to solve the test, Go!

Most of the questions were pretty straight forward, explain the event loop, a few programs that I need to evaluate their results, and then I saw the recursion question.

Question: Write a recursive function that prints the Fibonacci Sequence in ascending order. The function receives as parameters the last and second last numbers of the sequence. Example: printFibonacci(55,34) -> 1 1 2 3 5 8 13 21 34 55

If you donā€™t already know the answer, take a moment to think about the solution.

At first I thought ā€œItā€™s easy, Iā€™ve got this!ā€, but then I started to struggle, Iā€™ve already spent most of my time solving the other questions, I had around five minutes left to solve the question, but I couldnā€™t come up with a solution. All I could think of was the basic Fibonacci recursion that prints in descending order. I couldnā€™t figure out how to print the results in ascending order, and I ended up failing miserably.

Fibonacci Descending Order

Copy
// The code will print: 55 34 21 13 8 5 3 2 1
function printFibonacci(last, secondLast) {
if (last == 1 && secondLast == 0) {
return;
}
console.log(last);
// Moving to the next fibonacci numbers
return printFibonacci(secondLast, last - secondLast);
}
printFibonacci(55, 34);

In the above solution Iā€™ve made two mistakes:

  1. Fibonacci sequence for this question starts with the values: 1 1 ā€¦ My stopping condition was correct, but it would only print ā€œ1ā€³ one time.

  2. Iā€™ve used ā€œreturnā€, therefore any code written after that return statement will not be executed. This was the error that prevented me from solving the question.

After the time was up, I sat down with the team leader to go over the test. Apparently this question was the most important one for him, because itā€™s meant to show him my thought process, therefore I believe this was the question that cost me the job offer. I havenā€™t encountered such a recursion since my first year at college studying computer science.

We started discussing options to solve the question, and Iā€™m trying to show him my thought process, but still I was not able to arrive at a solution.

Lets go over the solution together so you wonā€™t fail at this question like I did.

Solution

Copy
// The code will print: 1 1 2 3 5 8 13 21 34 55
function printFibonacci(last, secondLast) {
// The code above the recursive function call will be in descending order.
if (last == 1 && secondLast == 0) {
console.log(1);
return;
}
// Recursive function call, Moving to the next fibonacci numbers
printFibonacci(secondLast, last - secondLast);
// The code below the recursive function call will be in ascending order.
console.log(last);
}
printFibonacci(55, 34);

Any code above the printFibonacci recursive function call will run in descending order.

Any code below printFibonacci recursive function call will run in ascending order.

Lets look at the call stack to truly understand what happens:

Recursion Call Stack

Every time we reach the recursive function call (PrintFibonacci) we go one level deeper inside the recursion. When we reach the last function call in the recursion we start running each of the calls weā€™ve put in our call stack.

We start from the last call (1,0), as seen in the image above, therefore printing the first Fibonacci value of the sequence (1) by hitting the stopping condition. Next, we go to the second function call (1,1) and print the second value of the sequence and so forth.

After grasping the idea that the order in which I put the recursive call shifts the order of code execution, I finally understood how it truly works.

I hope this will help you with your next interview.

Ā© 2020-present Sagi Liba. All Rights Reserved