Google Interview Question

Run Settings
LanguageJavaScript
Language Version
Run Command
//if sume of 2 items inside array is 8, return true. Otherwise, return false // array1 [1,2,3,4,9]; => false; // array1 [1,2,6,7,9]; => true; //Big O(n * 2); function hasPairWithSum(arr, sum) { //first loop will get the first item let arrLength = arr.length; for(let i= 0; i < arrLength - 1; i ++) { for(let j= 1; j< arrLength; j++) { if(arr[i] + arr[j] == sum) { return true; } } } return false; } function hasPairWithSum2(arr,sum) { const mySet = new Set(); const len = arr.length; for(let i = 0; i < len; i++) { if(mySet.has(arr[i])){ return true; } mySet.add(sum - arr[i]); } return false; }
Editor Settings
Theme
Key bindings
Full width
Lines