A coding interview question asked at Google
Tags: career, codenewbie, javascript, challenge
Published: 2019-12-02 04:50:36

Hey everyone! Hope you enjoyed solving last week’s challenge. In case you haven’t seen it, I’ll link last week’s article so you can go check it out.

[The article](https://dev.to/coderbyte/a-common-coding-interview-question-461f)
[The challenge on Coderbyte](https://coderbyte.com/editor/Find%20Intersection:JavaScript?utm_source=Dev.to&utm_medium=Article&utm_campaign=CodeReview&utm_term=2%20-%20Common%20coding%20interview&utm_content=Intro%20paragraph)

Here's a popular way to solve last week's challenge:

## Two index approach:
A more optimized solution (one that is possible because the strings of numbers are sorted) involves initializing two indexes at the start of both strings. Check to see if the element at the index in the first string is equal to, less than, or greater than the element at the index in the second string. If they are equal, push the value to the result array. Because the strings are sorted, if the element in the first string is less than the element in the second string, you can be sure the first element doesn’t exist in the second string. Therefore, you can increment the first index to look at the next value. If the element in the first string is greater than the element in the second string, you can be sure that the value in the second string doesn’t exist in the first string and can increment the second index to look at the next value. This might be clearer to see in code!

```javascript
 function intersection (arr) {
  const inBothArrays = []
  const [arr1, arr2] = arr.map((str) => str.split(', ').map((e) => parseInt(e)))

  let index1 = 0
  let index2 = 0

  while (index1 < arr1.length && index2 < arr2.length) {
    const elem1 = arr1[index1]
    const elem2 = arr2[index2]

    if (elem1 === elem2) {
      inBothArrays.push(elem1)
      index1++
      index2++
    } else if (elem1 > elem2) {
      index2++
    } else if (elem1 < elem2) {
      index1++
    }
  }

  return inBothArrays.join(',')
}
```


So for the example:

Calling `intersection([“3, 4, 7, 11, 15”, “1, 3, 5, 8, 11”]);` your function should return `“3,11”`.

Here is an illustration that might make this a little clearer.

![Pointer picture](https://thepracticaldev.s3.amazonaws.com/i/58gjjiks3voqy2uj7tlj.png)

Remember, this solution only works because the arrays are sorted. The time complexity of this solution is `O(n+m)`.

##This week's challenge:

For this week, we'll be solving a coding problem that was given in an actual Google phone screen interview. Remember to head over to [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) to submit your code!

Write a function that takes an array containing two strings where each string represents keypresses separated by commas. For this problem, a keypress can be either a printable character or a backspace (represented by `-B`). Your function should determine if the two strings of keypresses are equivalent. 

You can produce a printable string from such a string of keypresses by having backspaces erase one preceding character. Consider two strings of keypresses equivalent if they produce the same printable string. For example:

```javascript
checkEquivalentKeypresses(["a,b,c,d", "a,b,c,c,-B,d"]) // true
checkEquivalentKeypresses(["-B,-B,-B,c,c", "c,c"]) // true
checkEquivalentKeypresses(["", "a,-B,-B,a,-B,a,b,c,c,c,d"]) // false
 
```

Have fun and you got this!!

##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 :)