A JavaScript interview question asked at Facebook
Tags: codenewbie, career, javascript, challenge
Published: 2019-12-09 04:07:24

Wow what a week! Last week’s challenge was a big hit. In case you missed it, here's a link to last week’s article [here](https://dev.to/coderbyte/a-google-coding-interview-question-4h0f) and the challenge on [Coderbyte](https://coderbyte.com/editor/Equivalent%20Keypresses:JavaScript?utm_source=Dev.to&utm_medium=Article&utm_campaign=CodeReview&utm_term=3%20-%20Google%20interview%20question&utm_content=Intro%20paragraph).

Before I begin talking about the solution to the challenge, I also wanted to let you all know that we at Coderbyte want to hear from you! Have you just had a technical interview and want feedback on how you think you did? Email me at *liz.gross@coderbyte.com* with interview questions you've been asked and your answers and I'll get back to you with feedback on your solution. Looking forward to hearing from you all!

And now, without further ado, here is a common way to solve this google interview question:

##Stack approach:

When I first heard of this question, I immediately thought of using a stack. A stack is a basic data structure where insertion and deletion of elements takes place at the top of the stack. There are normally three basic operations that can be performed on a stack: 

1. inserting an item into a stack (`push`) 
2. deleting an item from the stack (`pop` off the top) 
3. displaying the contents of the stack

In javascript, implementing a stack can be as simple as using an array and its `push` and `pop` methods. This is an excellent choice of data structure for our problem. As you iterate through the keypresses, you can `push` them to a stack. As soon as you hit a backspace keypress, just `pop` the top item off the stack! Here that is in code:

```javascript
function removeBackspaces(arr) {
  const result = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === '-B' && result.length > 0) {
      result.pop();
    } else if (arr[i] !== '-B') {
      result.push(arr[i]);
    }
  }
  return result;
}

function checkEqualInputs(arr) {
  const [arr1, arr2] = arr.map((e) => e.split(','))
  const result1 = removeBackspaces(arr1);
  const result2 = removeBackspaces(arr2);
  // check if arrays are equal
  return result1.join('') === result2.join('');
}
```

##Big O:

This is a great solution because it is relatively cheap in terms of time and space. The run-time complexity for both time and space is `O(n+m)` where `n` is the length of the first string and `m` is the length of the second. You will only ever have to iterate through each string once, and store stacks with at most the lengths of each string. 

##This week’s challenge:
This week, we’ll be solving a coding problem that was given in an actual Facebook phone screen interview. Please comment below with your solutions! And be sure to look at the [Facebook Interview Questions](https://coderbyte.com/course/facebook-interview-questions?utm_source=Dev.to&utm_medium=Article&utm_campaign=CodeReview&utm_term=4%20-%20Facebook%20interview%20question&utm_content=Intro%20paragraph) course on Coderbyte for more Facebook related challenges!

Write a function that takes a DOM element and smoothly animates it from its current position to `distance` pixels to the right over `duration` milliseconds. Implement the following function, `animate(el, milliseconds, distance)`

For example, `animate(document.querySelector('#myDiv'), 2000, 100)` would move the element with id `myDiv` 100px to the right over 2 seconds. Implement this function without using jQuery or any other third-party libraries. 

![element moving](https://thepracticaldev.s3.amazonaws.com/i/24v2pr4p8zuv30124t4h.png)


Can’t wait to see what you all come up with. Have fun and happy coding!

##Our newsletter 📫
We’re going to be sending out a small, feature reveal snippet every time we release something big, so our community is the first to know when we break out something new. [Give us your email here] (https://coderbyte.typeform.com/to/fb7Yk8) and we'll add you to our "first to know" list :)