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 C++ code that uses two pointers to find if a pair sums to 10?
DSA C++
int arr[] = {1, 2, 3, 7, 8, 9}; int n = 6; int left = 0, right = n - 1; bool found = false; while (left < right) { int sum = arr[left] + arr[right]; if (sum == 10) { found = true; break; } else if (sum < 10) { left++; } else { right--; } } if (found) std::cout << "Pair found"; else std::cout << "No pair";
Attempts:
2 left
š” Hint
Think about how the two pointers move to find a pair that sums to 10.
ā Incorrect
The two pointer technique efficiently finds the pair (1,9) that sums to 10 without checking all pairs.
š§ Conceptual
intermediateWhy Two Pointer Technique is Faster than Brute Force
Which reason best explains why the two pointer technique is faster than brute force for sorted arrays?
Attempts:
2 left
š” Hint
Consider how the pointers move when sum is less or greater than target.
ā Incorrect
Two pointers move inward to find pairs, skipping unnecessary checks, unlike brute force which checks all pairs.
š§ Debug
advancedIdentify the Bug in Two Pointer Implementation
What error does this code produce when trying to find a pair summing to 15?
DSA C++
int arr[] = {1, 3, 5, 7, 9}; int n = 5; int left = 0, right = n - 1; while (left <= right) { int sum = arr[left] + arr[right]; if (sum == 15) { std::cout << "Found"; break; } else if (sum < 15) { left++; } else { right--; } } if (left > right) std::cout << "Not found";
Attempts:
2 left
š” Hint
Check the loop condition and if it allows pointers to cross correctly.
ā Incorrect
The loop condition 'left <= right' allows pointers to be equal, but no pair sums to 15, so it ends printing 'Not found'.
ā Predict Output
advancedOutput of Two Pointer Technique on Unsorted Array
What is the output of this code using two pointers on an unsorted array to find sum 10?
DSA C++
int arr[] = {5, 1, 4, 9, 2}; int n = 5; int left = 0, right = n - 1; bool found = false; while (left < right) { int sum = arr[left] + arr[right]; if (sum == 10) { found = true; break; } else if (sum < 10) { left++; } else { right--; } } if (found) std::cout << "Pair found"; else std::cout << "No pair";
Attempts:
2 left
š” Hint
Two pointer technique requires sorted arrays to work correctly.
ā Incorrect
Since the array is unsorted, the two pointer technique fails to find the pair even though (1,9) sums to 10.
š Application
expertTwo Pointer Technique to Find All Unique Pairs Summing to Target
Given a sorted array, which code snippet correctly prints all unique pairs that sum to 8 without duplicates?
Attempts:
2 left
š” Hint
Avoid duplicates by skipping repeated elements after finding a pair.
ā Incorrect
Option C correctly moves pointers and skips duplicates to print unique pairs summing to 8.
