The Recursion That Cost Me a Javascript’s Full Stack Position

Sagi Liba
I Read You Learn
Published in
4 min readFeb 10, 2020

Its 6 PM I’m tired and after a long day at work, I’ve arrived at the interview site and met the team leader 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 its easy i got this! but then i started to struggle, I’ve already spent most of my time solving the other questions, I’ve 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

The above code prints: 55 34 21 13 8 5 3 2 1

With 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 its supposed to show him my thought process, therefore i believe this was the question that cost me the job offer. I didn’t encounter such a recursion since my first year at college studying computer science.

we start discussing options to solve the question and i’m trying to show him my thought process but still i couldn’t arrive at a solution.

Lets go over the solution together so you won’t fail at this question like i did.

Solution:

The above code prints: 1 1 2 3 5 8 13 21 34 55

Section A comes before the call for starting the recursion (printFibonacci), any code that’s being placed in this section will be in descending order.

Section B comes after the call for starting the recursion (printFibonacci),
any code that’s being placed in this section will be in ascending order.

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

recursion’s call stack

After reaching 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) therefore printing the first Fibonacci value of the sequence (1) by hitting the stopping condition, afterwards 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 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.

I’m Excited To Introduce My First Course — SkilledJS Frontend Interview
With 70+ Lessons, 35+ Exercises & 15+ Quizzes.

It Has A KickAss React Interview Template,And 5 Amazing Modules,
that will prepare you for the Javascript interview, such as:
Professional Javascript, Professional React, Common Code Libraries, Industry Best-Practices, Interview Template Overview.

Course Link: Skilled JS Frontend Interview Course

--

--

Sagi Liba
I Read You Learn

Senior Frontend Developer, Creator of the Skilled JS Frontend Interview Course, and the "I Read. You Learn" Software Development Blog.