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 this TypeScript code that uses two pointers to find if a pair sums to 9?
DSA Typescript
function hasPairWithSum(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;
}
const result = hasPairWithSum([1, 2, 4, 4, 5, 6, 8], 9);
console.log(result);Attempts:
2 left
💡 Hint
Think about pairs that add up to 9 in the sorted array.
✗ Incorrect
The function uses two pointers starting at the ends of the sorted array. It finds the pair (1,8) which sums to 9 and returns true.
❓ Predict Output
intermediateResult of Merging Two Sorted Arrays Using Two Pointers
What is the output of this TypeScript code that merges two sorted arrays using two pointers?
DSA Typescript
function mergeSortedArrays(arr1: number[], arr2: number[]): number[] {
let i = 0, j = 0;
const merged: number[] = [];
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
merged.push(arr1[i]);
i++;
} else {
merged.push(arr2[j]);
j++;
}
}
while (i < arr1.length) {
merged.push(arr1[i]);
i++;
}
while (j < arr2.length) {
merged.push(arr2[j]);
j++;
}
return merged;
}
const result = mergeSortedArrays([1, 3, 5], [2, 4, 6]);
console.log(result);Attempts:
2 left
💡 Hint
The function merges two sorted arrays into one sorted array.
✗ Incorrect
The function compares elements from both arrays and pushes the smaller one, resulting in a fully sorted merged array.
🔧 Debug
advancedIdentify the Error in Two Pointer Palindrome Check
What error will this TypeScript code produce when checking if a string is a palindrome using two pointers?
DSA Typescript
function isPalindrome(s: string): boolean {
let left = 0;
let right = s.length - 1;
while (left <= right) {
if (s[left] !== s[right]) {
return false;
}
left++;
right--;
}
return true;
}
console.log(isPalindrome('racecar'));Attempts:
2 left
💡 Hint
Check the loop condition and indexing carefully.
✗ Incorrect
The code correctly compares characters from both ends moving inward and returns true for 'racecar'. No error occurs.
❓ Predict Output
advancedOutput of Removing Duplicates from Sorted Array Using Two Pointers
What is the output of this TypeScript code that removes duplicates from a sorted array in-place using two pointers?
DSA Typescript
function removeDuplicates(nums: number[]): number {
if (nums.length === 0) return 0;
let i = 0;
for (let j = 1; j < nums.length; j++) {
if (nums[j] !== nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
const nums = [1, 1, 2, 2, 3, 4, 4];
const length = removeDuplicates(nums);
console.log(length);
console.log(nums.slice(0, length));Attempts:
2 left
💡 Hint
The function returns the count of unique elements and modifies the array in-place.
✗ Incorrect
The function moves unique elements to the front and returns the count 4. The first 4 elements are [1, 2, 3, 4].
🧠 Conceptual
expertTwo Pointer Technique Application Scenario
Which of the following problems is best solved using the two pointer technique on arrays?
Attempts:
2 left
💡 Hint
Two pointers work well when the array is sorted and you need to find pairs.
✗ Incorrect
The two pointer technique efficiently finds pairs in a sorted array that sum to a target by moving pointers inward.
