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 find if a pair sums to 10?
DSA C
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 7, 8, 9}; int left = 0, right = 5; int target = 10; 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("Pair found: %d + %d = %d\n", arr[left], arr[right], target); } else { printf("No pair found\n"); } return 0; }
Attempts:
2 left
💡 Hint
Trace the pointers moving inward and check sums.
✗ Incorrect
The two pointer technique starts with left at 0 and right at 5. Sum of arr[0]+arr[5] = 1+9=10 matches target, so the code breaks immediately. The code finds the first pair that sums to 10, which is 1+9=10. So option A is correct.
🧠 Conceptual
intermediate1:30remaining
Why Two Pointer Technique is Faster than Brute Force
Which of the following best explains why the two pointer technique is faster than brute force for sorted arrays?
Attempts:
2 left
💡 Hint
Think about how the pointers move and reduce checks.
✗ Incorrect
The two pointer technique uses two indices moving inward from both ends. It compares sums and moves pointers to skip unnecessary pairs, reducing time complexity from O(n^2) to O(n).
🔧 Debug
advanced2:00remaining
Find the Bug in Two Pointer Implementation
What error does the following code produce when trying to find a pair summing to 15 in a sorted array?
DSA C
#include <stdio.h> int main() { int arr[] = {1, 3, 5, 7, 9}; int left = 0, right = 4; int target = 15; 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("Pair found: %d + %d = %d\n", arr[left], arr[right], target); } else { printf("No pair found\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and array values carefully.
✗ Incorrect
The loop condition uses left <= right, which is valid. The array has no pair summing to 15, so it prints 'No pair found'. No infinite loop or segmentation fault occurs.
🚀 Application
advanced2:30remaining
Using Two Pointer to Remove Duplicates in Sorted Array
Given a sorted array, which code snippet correctly removes duplicates in-place and returns the new length?
Attempts:
2 left
💡 Hint
Check loop bounds and initial indices carefully.
✗ Incorrect
Option B correctly uses two pointers i and j, starts i at 0, j at 1, and updates i and array when a new unique element is found. It returns i+1 as the new length.
🧠 Conceptual
expert1:30remaining
Time Complexity Comparison of Two Pointer vs Brute Force
What is the time complexity of the two pointer technique compared to brute force when finding pairs in a sorted array?
Attempts:
2 left
💡 Hint
Consider how many times each pointer moves in two pointer approach.
✗ Incorrect
Two pointer technique moves each pointer at most n times, resulting in O(n) time. Brute force checks all pairs, O(n^2).
