# 4 Ways to Solve a Google Interview Question in JavaScript

## A companion to Google’s official mock-interview video for developers. Image Credit: Ekaphon maneechot/Shutterstock

# Solution 1: Brute Force

`const findSum = (arr, val) => {  for (let i = 0; i < arr.length; i++) {    for (let j = 0; j < arr.length; j++) {      if (i !== j && arr[i] + arr[j] === val) {        return true;      };    };  };  return false;};`

# Solution 2: Binary Search

`const findSum = (arr, val) => {  for (let i = 0; i < arr.length; i++){    if (binarySearch(removeIndex(arr, i), val - arr[i])) {      return true;    }  };  return false;};const removeIndex = (arr, i) => {  return arr.slice(0, i).concat(arr.slice(i + 1, arr.length));};const binarySearch = (arr, val) => {  let start = 0;  let end = arr.length - 1;  let pivot = Math.floor(arr.length / 2);    while (start < end) {    if (val < arr[pivot]) {      end = pivot - 1;    } else if (val > arr[pivot]) {      start = pivot + 1;    };    pivot = Math.floor((start + end) / 2);    if (arr[pivot] === val) {      return true;    }  };  return false;};`

# Solution 3: Linear Time

`const findSum = (arr, val) => {  let start = 0;  let end = arr.length - 1;  while (start < end) {    let sum = arr[start] + arr[end];    if (sum > val) {      end -= 1;    } else if (sum < val) {      start += 1;    } else {      return true;    };  };  return false;};`

# Solution 4 — Linear Time and Space

`const findSum = (arr, val) => {  let searchValues = new Set();  searchValues.add(val - arr);  for (let i = 1, length = arr.length; i < length; i++) {    let searchVal = val - arr[i];    if (searchValues.has(arr[i])) {      return true;    } else {      searchValues.add(searchVal);    }  };  return false;};`
`const findSum = (arr, sum) =>  arr.some((set => n => set.has(n) || !set.add(sum - n))(new Set));`

# Conclusion

Writer and developer based in London. On Medium, I write about JavaScript and web development 💻