Airbnb Coding Interview Questions
By: Anh Dang
Tags: JavaScript, Interview, Technology, Airbnb, Interview Questions

Airbnb Coding Interview Questions

(Updated April 6, 2020)

Daily coding problems

They are a wide variety of questions inspired by real programming interviews, with in-depth solutions that clearly take you through each core concept.

Get exceptionally good at coding interviews by solving one problem every day.

We will together solve these problems using JavaScript.

Problem #1

Question

Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Numbers can be 0 or negative. For example, [2, 4, 6, 2, 5] should return 13 , since we pick 2, 6, and [5, 1, 1, 5] should return 10 , since we pick 5 and 5.

Can you do this in O(N) time and constant space?

Solution

Here is my solution. It is simple for anybody to understand.

const i1 = [2, 4, 6, 2, 5];

const i2 = [5, 1, 1, 5];



const nonAdjSum = (input) => {

let incl = 0;

let excl = 0;

let new_excl;

input.forEach( (el) => {

new_excl = excl > incl ? excl : incl;

incl = excl + el;

excl = new_excl;

})



return excl > incl ? excl : incl;



}



nonAdjSum(i1)

<< 13 nonAdjSum(i2)

<< 10

Problem #2

Question

These rules of the Collatz conjecture.

If a number is odd, the next transform is 3*n+1

If a number is even, the next transform is n/2

The number is finally transformed into 1.

The step is how many transforms needed for a number turned into 1.

Given an integer n, output the max steps of transform number in [1, n] into 1.

Solution

First of all, we write the findSteps(num) function.

const findSteps = function (num) {

if (num <= 1) return 1

if (num % 2 == 0)

return 1 + findSteps(num / 2)

return 1 + findSteps(3 * num + 1)

}

Then, we find max steps to transform by implementing the findLongestSteps(num) function.

const findLongestSteps = function (num) {



if (num < 1) return 0



var res = 0

for (let i = 1; i <= num; i++) {

t = findSteps(i)

res = Math.max(res, t)

}



return res

}

Finally, we try to test this solutions.

console.log(findLongestSteps(37))

<< 112 console.log(findLongestSteps(101))

<< 119

Problem #3

Question

Implement a queue with a number of arrays, in which each array has fixed size, MAX_QUEUE_SIZE .

Solution

From the generic description of the queue, we know we need, at least:

offer(num) function

function poll() function.

function. isEmpty() and isFull() functions.

The pieces of data we need for our array implementation of the simple queue are: an array and a count . Will these pieces be sufficient? Let’s look at an example to find out. We’ll start with a queue with 3 elements.

queue (made up of 'contents' and 'count')

----------------- -----

| a | b | c | | | 3 |

----------------- -----

0 1 2 3 count

contents

Where a is at the front and c is at the rear . Now, we poll a new element with: queue.offer('e') .

queue (made up of 'contents' and 'count')

----------------- -----

| a | b | c | d | | 4 |

----------------- -----

0 1 2 3 count

contents

All seems well. How about if we remove an element with: queue.poll() .

queue (made up of 'contents' and 'count')

----------------- ----- -----

| | b | c | d | | 3 | | a |

----------------- ----- -----

0 1 2 3 count ch

contents

Unfortunately, we have a problem because the front of the queue is no longer at array position 0. One solution would be to move all the elements down one, giving.

queue (made up of 'contents' and 'count')

----------------- -----

| b | c | d | | | 3 |

----------------- -----

0 1 2 3 count

contents

We reject that solution though because it is too expensive to move everything down every time we remove an element. Instead, can we use an additional piece of information to keep track of the front?

Yes! We can use the index of the element at the front, giving.

queue (made up of 'contents', 'front' and 'count')

----------------- ----- -----

| | b | c | d | | 1 | | 3 |

----------------- ----- -----

0 1 2 3 front count

contents

Now, what if we enter another element with: queue.offer('e') ? Currently, the rear of the queue holds 'd' and is at the end of the array. Where will we put 'e' ? We’ve already said that moving everything down is too expensive. An alternative would be to use the array in a circular fashion . In other words, when we hit the end of the array, we wrap around and use the beginning. Now, with this choice for entering 'e' , the fields look like:

queue (made up of 'contents', 'front' and 'count')

----------------- ----- -----

| e | b | c | d | | 1 | | 4 |

----------------- ----- -----

0 1 2 3 front count

contents

Finally, how would it look like after: queue.poll() ?

queue (made up of 'contents', 'front' and 'count')

----------------- ----- ----- -----

| e | | c | d | | 2 | | 3 | | b |

----------------- ----- ----- -----

0 1 2 3 front count ch

contents

As we’ve seen, one choice for the set of data needed for the queue is: an array , a front index and a count .

There is another possibility. Instead, we could replace the count with the location of the rear, thus using the following pieces of data: an array , a front index , a rear index .

Then, these pieces of data would reflect the state of the queue in the following way…Starting out with a queue with 4 elements…

queue (made up of 'contents', 'front' and 'rear')

----------------- ----- -----

| a | b | c | d | | 0 | | 3 |

----------------- ----- -----

0 1 2 3 front rear

contents

Now, remove one with queue.poll() , giving:

queue (made up of 'contents', 'front' and 'rear')

----------------- ----- -----

| | b | c | d | | 1 | | 3 |

----------------- ----- -----

0 1 2 3 front rear

contents

Now, add one with queue.offer('e') , giving:

queue (made up of 'contents', 'front' and 'rear')

----------------- ----- -----

| e | b | c | d | | 1 | | 0 |

----------------- ----- -----

0 1 2 3 front rear

contents

Can you implement this algorithm by using JavaScript?

Easy, right?