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 check if any two numbers sum to 10?
DSA C#
int[] arr = {1, 2, 3, 7, 8, 9}; int left = 0, right = arr.Length - 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--; } } Console.WriteLine(found);
Attempts:
2 left
💡 Hint
Think about pairs from start and end moving inward to find sum 10.
✗ Incorrect
The pair (1,9) sums to 10, so the loop sets found to true and breaks.
❓ Predict Output
intermediateResult of Merging Two Sorted Arrays Using Two Pointers
What is the output list after merging these two sorted arrays using two pointers?
DSA C#
int[] arr1 = {1, 3, 5}; int[] arr2 = {2, 4, 6}; List<int> merged = new List<int>(); int i = 0, j = 0; while (i < arr1.Length && j < arr2.Length) { if (arr1[i] < arr2[j]) { merged.Add(arr1[i]); i++; } else { merged.Add(arr2[j]); j++; } } while (i < arr1.Length) { merged.Add(arr1[i]); i++; } while (j < arr2.Length) { merged.Add(arr2[j]); j++; } Console.WriteLine(string.Join(", ", merged));
Attempts:
2 left
💡 Hint
Two pointers pick smaller elements from each array step by step.
✗ Incorrect
The code merges sorted arrays by always adding the smaller current element from either array.
🔧 Debug
advancedIdentify the Error in Two Pointer Palindrome Check
What error does this code produce when checking if a string is a palindrome using two pointers?
DSA C#
string s = "racecar"; int left = 0, right = s.Length - 1; while (left < right) { if (s[left] != s[right]) { Console.WriteLine("Not palindrome"); break; } left++; right--; } Console.WriteLine("Palindrome");
Attempts:
2 left
💡 Hint
Check what happens after the loop breaks.
✗ Incorrect
The code prints "Palindrome" after the loop regardless of the break, so it prints both messages if not palindrome.
🚀 Application
advancedFind the Number of Unique Pairs with Given Sum
Given a sorted array, how many unique pairs sum to 7 using two pointers?
DSA C#
int[] arr = {1, 2, 3, 4, 4, 5, 6}; int left = 0, right = arr.Length - 1; int count = 0; while (left < right) { int sum = arr[left] + arr[right]; if (sum == 7) { count++; int leftVal = arr[left]; int rightVal = arr[right]; while (left < right && arr[left] == leftVal) left++; while (left < right && arr[right] == rightVal) right--; } else if (sum < 7) { left++; } else { right--; } } Console.WriteLine(count);
Attempts:
2 left
💡 Hint
Count pairs (1,6), (2,5), (3,4) ignoring duplicates.
✗ Incorrect
Pairs summing to 7 are (1,6), (2,5), and (3,4). The code skips duplicates correctly.
🧠 Conceptual
expertWhy Two Pointer Technique is Efficient for Sorted Arrays?
Why is the two pointer technique efficient for problems like finding pairs with a target sum in sorted arrays?
Attempts:
2 left
💡 Hint
Think about how scanning from both ends avoids nested loops.
✗ Incorrect
Two pointers move inward, checking sums in one pass, avoiding nested loops and reducing complexity.
