Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 9, 2019 04:07 am GMT

A JavaScript interview question asked at Facebook

Wow what a week! Last weeks challenge was a big hit. In case you missed it, here's a link to last weeks article here and the challenge on Coderbyte.

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 [email protected] 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:

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 weeks challenge:

This week, well 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 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

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


Original Link: https://dev.to/coderbyte/a-javascript-interview-question-asked-at-facebook-27po

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To