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 code that uses two pointers to check if any two numbers sum to 10?
DSA Javascript
const arr = [1, 2, 3, 7, 8, 9]; let left = 0; let right = arr.length - 1; let found = false; while (left < right) { const sum = arr[left] + arr[right]; if (sum === 10) { found = true; break; } else if (sum < 10) { left++; } else { right--; } } console.log(found);
Attempts:
2 left
💡 Hint
Think about pairs that add up to 10 in the sorted array.
✗ Incorrect
The pair (1, 9) sums to 10, so the code sets found to true and breaks the loop.
❓ Predict Output
intermediateResulting Array After Removing Duplicates Using Two Pointers
What is the output array after running this two pointer code that removes duplicates from a sorted array?
DSA Javascript
const nums = [1, 1, 2, 2, 3, 4, 4]; let i = 0; for (let j = 1; j < nums.length; j++) { if (nums[j] !== nums[i]) { i++; nums[i] = nums[j]; } } const result = nums.slice(0, i + 1); console.log(result);
Attempts:
2 left
💡 Hint
The code keeps unique elements at the start of the array.
✗ Incorrect
The two pointers move through the array, copying unique elements forward. The final slice returns only unique elements.
❓ Predict Output
advancedOutput of Merging Two Sorted Arrays Using Two Pointers
What is the output of merging two sorted arrays using two pointers?
DSA Javascript
const arr1 = [1, 3, 5]; const arr2 = [2, 4, 6]; const merged = []; let i = 0, j = 0; 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++; } console.log(merged);
Attempts:
2 left
💡 Hint
Two pointers pick the smaller element from each array step by step.
✗ Incorrect
The code merges two sorted arrays into one sorted array by comparing elements at pointers and pushing the smaller one.
❓ Predict Output
advancedOutput of Two Pointer Technique to Reverse a String Array
What is the output after reversing the array using two pointers?
DSA Javascript
const arr = ['a', 'b', 'c', 'd', 'e']; let left = 0; let right = arr.length - 1; while (left < right) { const temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } console.log(arr);
Attempts:
2 left
💡 Hint
Two pointers swap elements from start and end moving inward.
✗ Incorrect
The code swaps elements at left and right pointers until they meet, reversing the array.
🧠 Conceptual
expertTwo Pointer Technique for Finding All Unique Triplets Summing to Zero
Which of the following best describes the two pointer approach to find all unique triplets in an array that sum to zero?
Attempts:
2 left
💡 Hint
Think about how sorting helps and how two pointers can find pairs efficiently.
✗ Incorrect
Sorting allows skipping duplicates and using two pointers to find pairs that sum to a target efficiently, reducing complexity.
