Given the array [1, 3, 4, 5, 7] and target sum 8, what is the output of the two functions below?
Function bruteForceSum checks all pairs for sum. Function twoPointerSum uses two pointers on sorted array.
function bruteForceSum(arr: number[], target: number): boolean {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] === target) {
return true;
}
}
}
return false;
}
function twoPointerSum(arr: number[], target: number): boolean {
let left = 0;
let right = arr.length - 1;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === target) {
return true;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return false;
}
console.log(bruteForceSum([1, 3, 4, 5, 7], 8));
console.log(twoPointerSum([1, 3, 4, 5, 7], 8));Check if any two numbers add up to 8 in the array.
Both functions find the pair (1,7) that sums to 8, so both return true.
What is the output of the twoPointerSum function when called with an unsorted array [9, 1, 7, 2] and target 8?
function twoPointerSum(arr: number[], target: number): boolean {
let left = 0;
let right = arr.length - 1;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === target) {
return true;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return false;
}
console.log(twoPointerSum([9, 1, 7, 2], 8));Two pointer technique requires sorted array to work correctly.
The array is unsorted, so the two pointer method misses pairs like (1,7). It returns false.
Which of the following best explains why the two pointer technique is more efficient than brute force for finding pairs that sum to a target in a sorted array?
Think about how many pairs are checked in each method.
Two pointer technique moves pointers inward and checks pairs in linear time O(n), while brute force checks all pairs in O(n²).
What error will the following code produce when run?
function twoPointerSum(arr: number[], target: number): boolean {
let left = 0;
let right = arr.length;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === target) {
return true;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return false;
}
twoPointerSum([1, 2, 3, 4], 5);Check the initial value of right and array indexing.
Although right is initialized to arr.length causing an out-of-bounds access and undefined, resulting in NaN for sum, JavaScript comparisons involving NaN always evaluate to false. Thus, the else branch is taken, decrementing right, and the function proceeds correctly to return true. No error is thrown.
Given a sorted array [-2, 0, 1, 1, 2, 3], which option correctly outputs all unique pairs that sum to 2 using the two pointer technique?
function findPairs(arr: number[], target: number): Array<[number, number]> {
const result: Array<[number, number]> = [];
let left = 0;
let right = arr.length - 1;
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === target) {
result.push([arr[left], arr[right]]);
left++;
right--;
while (left < right && arr[left] === arr[left - 1]) left++;
while (left < right && arr[right] === arr[right + 1]) right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return result;
}
console.log(findPairs([-2, 0, 1, 1, 2, 3], 2));Check which pairs actually sum to 2 and avoid duplicates.
Pairs that sum to 2 are (0,2) and (1,1). There is no 4 in the array, so options with 4 are invalid.
