Challenge - 5 Problems
Two Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of Two Pointer Sum Check
What is the output of the following JavaScript code that uses two pointers to find if a pair sums to 10?
DSA Javascript
function hasPairWithSum(arr, target) {
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(hasPairWithSum([1, 3, 4, 5, 7, 10, 12], 10));Attempts:
2 left
š” Hint
Think about how the two pointers move to find the sum.
ā Incorrect
The two pointer technique efficiently checks pairs by moving pointers inward. Here, 3 + 7 equals 10, so it returns true.
š§ Conceptual
intermediateWhy Two Pointer Technique is Faster than Brute Force
Which of the following best explains why the two pointer technique is faster than brute force for sorted arrays?
Attempts:
2 left
š” Hint
Consider how many pairs brute force checks versus two pointers.
ā Incorrect
Brute force checks all pairs (O(n^2)). Two pointers move inward based on sum comparison, checking fewer pairs (O(n)).
ā Predict Output
advancedOutput of Two Pointer Technique with Duplicates
What is the output of this code that finds all unique pairs summing to 8 using two pointers?
DSA Javascript
function twoSumPairs(arr, target) {
arr.sort((a, b) => a - b);
let left = 0, right = arr.length - 1;
const result = [];
while (left < right) {
const sum = arr[left] + arr[right];
if (sum === target) {
result.push([arr[left], arr[right]]);
while (arr[left] === arr[left + 1]) left++;
while (arr[right] === arr[right - 1]) right--;
left++;
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return result;
}
console.log(twoSumPairs([1, 3, 3, 4, 5, 5, 7, 8], 8));Attempts:
2 left
š” Hint
Duplicates are skipped after adding a pair.
ā Incorrect
The code finds pairs (1,7) and (3,5) once each, skipping duplicates to avoid repeated pairs.
š§ Debug
advancedIdentify the Error in Two Pointer Implementation
What error does the following code produce when trying to find if a pair sums to 30?
DSA Javascript
function checkSum(arr, target) {
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(checkSum([2, 4, 7, 11, 15], 30));Attempts:
2 left
š” Hint
Check the loop condition and pointer movement.
ā Incorrect
The loop condition allows left to equal right, causing the self-pair (15,15) to be checked which is invalid. There is no distinct pair summing to 30, so it returns false.
š Application
expertTwo Pointer Technique for Maximum Water Container
Given the heights array, what is the maximum water container area calculated by the two pointer approach?
DSA Javascript
function maxArea(height) {
let left = 0, right = height.length - 1;
let maxArea = 0;
while (left < right) {
const width = right - left;
const minHeight = Math.min(height[left], height[right]);
const area = width * minHeight;
maxArea = Math.max(maxArea, area);
if (height[left] < height[right]) left++;
else right--;
}
return maxArea;
}
console.log(maxArea([1,8,6,2,5,4,8,3,7]));Attempts:
2 left
š” Hint
Calculate area as width times minimum height between pointers.
ā Incorrect
The maximum area is between indices 1 and 8: width 7, min height 7, area 49.
