JavaScript Interview Coding Questions — 3

Halil Can Ozcelik
3 min readJan 8, 2020

--

I am trying to explain some possible coding questions in software developer interviews. I will mention recursion and array mutation in this third article. These two topics are important in functional programming paradigm. Also, the last example is about prototypal inheritance which is crucial to understand inheritance in JavaScript.

  1. Write a recursive function to calculate the total of numbers between 1 to n?

n will be the parameter of our function. So we should call this calculator function until reaching to 1 which is our end point. So, one of the possible effective solutions will be below code:

function calculateTotal(number, total = 1) {  return number > 1 ?    calculateTotal(number - 1, total + number) :    total;}console.log(calculateTotal(10));

You can examine the code via this link.

2. Write a recursive factorial calculator function.

We can easily adapt same logic to factorial calculation as below:

function factorial(number, product = 1) {  return number > 0 ?    factorial(number - 1, product * number) :    product;}console.log(factorial(5));

You can examine the code via this link.

!! The recursive functions above will cause stack overflow error for large inputs. In order to prevent it, Trampoline Pattern can be used as below:

// recursive optimization to prevent stack overflow error
function trampoline(fn) {
return (...args) => {
let result = fn(...args);
while (typeof result === 'function') {
result = result();
}
return result;
};
}

// Write a recursive function to calculate the total of numbers between 1 to n?
function calculateTotal(number, total = 1) {
return number > 1 ?
() => calculateTotal(number - 1, total + number) :
total;
}

const totalCalculator = trampoline(calculateTotal);
console.log(totalCalculator(100000));

// Write a recursive factorial calculator function
function factorial(number, product = 1) {
return number > 0 ?
() => factorial(number - 1, product * number) :
product;
}

const factorialCalculator = trampoline(factorial);
console.log(factorialCalculator(100));

3. This one is about mutator methods in JavaScript arrays. Immutability of variables is an important topic in functional programming.

var arr = [1, 2, 3, 7, 4];// Which of the followings will mutate variables?// Find a functional alternative for mutator ones.arr.push(5);          => mutatorarr.shift();          => mutatorarr.concat(6, 7);     => non-mutatorarr.map(a => a * a);  => non-mutatorarr.sort();           => mutator

And these can be alternative solutions for mutator ones.

var arr = [1, 2, 3, 7, 4];// Which of the followings will mutate variables?// Find a functional alternative for mutator ones.arr.push(5);          => arr.concat(5);arr.shift();          => arr.slice(1);arr.concat(6, 7);     => non-mutatorarr.map(a => a * a);  => non-mutatorarr.sort();           => arr.concat().sort()

You can examine the code via this link.

4. This one is to examine your understanding about Prototypal Inheritance.

function Person() {}// 1st definition for 'sayHi' methodPerson.prototype.sayHi = function () {  console.log('Hi!');};var person = new Person();// What will be the printed message?person.sayHi();// 2nd definition for 'sayHi' methodPerson.prototype.sayHi = function () {  console.log('Hello!');};// What will be the printed message?person.sayHi();// What will be returned?person.hasOwnProperty('sayHi');

The output will be below:

Hi!
Hello!
false

person object doesn’t have own sayHi() method because Person function doesn’t have any method. When we instantiate an object with new keyword, it inherits all prototype methods of the function as its __proto__ property. So, in first execution of sayHi() the defined one is logging Hi! so it is executed. But after second definition of sayHi() the newer one will be called. Because, person.sayHi() points to the same function due to prototypal inheritance. Finally, person.hasOwnProperty('sayHi') returns false because this is not a property of person object, it is inherited by prototype chain.

You can the code via this link.

My some other articles:

  1. JavaScript Interview Coding Questions — 1
  2. JavaScript Interview Coding Questions — 2
  3. Chrome DevTool Tips

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response