Challenge - 5 Problems
Two Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Two Pointer Sum Check
What is the output of the following C code that uses two pointers to check if any two numbers in a sorted array sum to a target value?
DSA C
#include <stdio.h> int main() { int arr[] = {1, 3, 4, 5, 7, 10, 12}; int target = 9; int left = 0, right = 6; int found = 0; while (left < right) { int sum = arr[left] + arr[right]; if (sum == target) { found = 1; break; } else if (sum < target) { left++; } else { right--; } } if (found) { printf("Found: %d + %d = %d\n", arr[left], arr[right], target); } else { printf("Not Found\n"); } return 0; }
Attempts:
2 left
💡 Hint
Think about how the two pointers move based on the sum compared to the target.
✗ Incorrect
The two pointers start at the ends: 1 and 12 sum to 13 which is greater than 9, so right pointer moves left. Then 1 and 10 sum to 11, still greater, right moves left. Then 1 and 7 sum to 8, less than 9, left moves right. Then 3 and 7 sum to 10, greater, right moves left. Then 3 and 5 sum to 8, less, left moves right. Then 4 and 5 sum to 9, found the pair.
🧠 Conceptual
intermediate1:30remaining
Two Pointer Technique Purpose
What is the main advantage of using the two pointer technique on a sorted array compared to a nested loop approach?
Attempts:
2 left
💡 Hint
Think about how many times each element is visited in two pointer vs nested loops.
✗ Incorrect
The two pointer technique moves pointers inward in a single pass, checking pairs efficiently, reducing time complexity from quadratic to linear.
🔧 Debug
advanced2:00remaining
Identify the Bug in Two Pointer Implementation
What error will the following code produce when trying to find if two numbers sum to target in a sorted array?
DSA C
#include <stdio.h> int main() { int arr[] = {2, 4, 6, 8, 10}; int target = 14; int left = 0, right = 4; while (left <= right) { int sum = arr[left] + arr[right]; if (sum == target) { printf("Found: %d + %d = %d\n", arr[left], arr[right], target); return 0; } else if (sum < target) { left++; } else { right--; } } printf("Not Found\n"); return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and pointer movements carefully.
✗ Incorrect
The loop condition 'left <= right' is valid here because when left == right, sum is arr[left]*2 which won't equal target 14. The pointers move correctly and the pair 4 + 10 = 14 is found.
❓ Predict Output
advanced2:00remaining
Output of Two Pointer Removing Duplicates
What is the output of this C code that removes duplicates from a sorted array using two pointers?
DSA C
#include <stdio.h> int main() { int arr[] = {1, 1, 2, 2, 3, 4, 4, 5}; int n = 8; int i = 0, j = 1; while (j < n) { if (arr[i] != arr[j]) { i++; arr[i] = arr[j]; } j++; } printf("Array after removing duplicates: "); for (int k = 0; k <= i; k++) { printf("%d ", arr[k]); } printf("\n"); return 0; }
Attempts:
2 left
💡 Hint
Check how the pointers move and how duplicates are skipped.
✗ Incorrect
Pointer i tracks the last unique element index. Pointer j scans forward. When a new unique element is found, i increments and arr[i] is updated. Result is unique elements at start: 1 2 3 4 5.
🧠 Conceptual
expert1:30remaining
Two Pointer Technique Limitations
Which of the following is a limitation of the two pointer technique on arrays?
Attempts:
2 left
💡 Hint
Think about the assumptions needed for two pointers to work correctly.
✗ Incorrect
Two pointer technique relies on sorted or ordered arrays to move pointers logically. Without order, it cannot guarantee correct results efficiently.
