Challenge - 5 Problems
Two Pointer Master
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 C++ code that uses two pointers to check if any two numbers sum 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 << "Yes" << std::endl; } else { std::cout << "No" << std::endl; }
Attempts:
2 left
💡 Hint
Think about pairs that add up to 10 in the sorted array.
✗ Incorrect
The two pointers start at the beginning and end of the array. The pair (1,9) sums to 10, so the output is "Yes".
❓ Predict Output
intermediateResulting Array After Removing Duplicates Using Two Pointers
What is the output array after running this C++ code that removes duplicates from a sorted array using two pointers?
DSA C++
int arr[] = {1, 1, 2, 2, 3, 4, 4, 5}; int n = 8; int j = 0; for (int i = 1; i < n; i++) { if (arr[i] != arr[j]) { j++; arr[j] = arr[i]; } } for (int k = 0; k <= j; k++) { std::cout << arr[k] << " "; }
Attempts:
2 left
💡 Hint
Duplicates are overwritten by unique elements as j moves forward.
✗ Incorrect
The code moves unique elements forward and prints only unique values: 1 2 3 4 5.
🔧 Debug
advancedIdentify the Error in Two Pointer Partition Code
What error does the following C++ code produce when trying to partition an array around a pivot using two pointers?
DSA C++
int arr[] = {5, 2, 9, 1, 5, 6}; int n = 6; int pivot = 5; int left = 0, right = n - 1; while (left <= right) { while (arr[left] < pivot) left++; while (arr[right] > pivot) right--; if (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } else { break; } } for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Check the loop condition and pointer updates carefully.
✗ Incorrect
The condition 'left <= right' with 'if (left < right)' inside causes the pointers to get stuck when left == right, leading to an infinite loop.
❓ Predict Output
advancedOutput of Merging Two Sorted Arrays Using Two Pointers
What is the output of the following C++ code that merges two sorted arrays using two pointers?
DSA C++
int arr1[] = {1, 3, 5}; int arr2[] = {2, 4, 6}; int n1 = 3, n2 = 3; int i = 0, j = 0; std::vector<int> merged; while (i < n1 && j < n2) { if (arr1[i] < arr2[j]) { merged.push_back(arr1[i++]); } else { merged.push_back(arr2[j++]); } } while (i < n1) merged.push_back(arr1[i++]); while (j < n2) merged.push_back(arr2[j++]); for (int x : merged) std::cout << x << " ";
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: 1 2 3 4 5 6.
🧠 Conceptual
expertMinimum Number of Operations to Make Array Non-Decreasing Using Two Pointers
Given an array, you want to make it non-decreasing by incrementing elements. Using a two pointer approach, what is the minimum number of increments needed for the array [3, 2, 5, 1, 7]?
Attempts:
2 left
💡 Hint
Increment elements that are smaller than the previous to match or exceed it.
✗ Incorrect
Increment 2 to 3 (+1), 1 to 5 (+4), total increments = 1 + 4 + 1 (for 5 to 5) = 6.
