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 the following Java code that uses two pointers to check if any two numbers in a sorted array sum to a target?
DSA Java
public class TwoPointerSum { public static boolean hasPairWithSum(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left < right) { int sum = arr[left] + arr[right]; if (sum == target) { return true; } else if (sum < target) { left++; } else { right--; } } return false; } public static void main(String[] args) { int[] arr = {1, 3, 4, 5, 7, 10, 12}; System.out.println(hasPairWithSum(arr, 9)); } }
Attempts:
2 left
š” Hint
Think about pairs that add up to 9 in the sorted array.
ā Incorrect
The two pointer technique moves pointers inward to find pairs summing to the target efficiently. Here, 4 + 5 = 9, so it returns true.
š§ Conceptual
intermediateWhy Two Pointer 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
Think about how many pairs brute force checks versus two pointer.
ā Incorrect
Brute force checks all pairs (O(n^2)). Two pointer moves inward, checking fewer pairs (O(n)).
š§ Debug
advancedFind the Bug in Two Pointer Implementation
What error will this code produce when run, and why?
DSA Java
public class BuggyTwoPointer { public static boolean hasPairWithSum(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left < right) { int sum = arr[left] + arr[right]; if (sum == target) { return true; } else if (sum < target) { left++; } else { right--; } } return false; } public static void main(String[] args) { int[] arr = {2, 4, 6, 8, 10}; System.out.println(hasPairWithSum(arr, 14)); } }
Attempts:
2 left
š” Hint
Check the initial value of right pointer.
ā Incorrect
Array indices go from 0 to length-1. Starting right at length causes out of bounds access.
š Application
advancedUsing Two Pointer to Merge Two Sorted Arrays
What is the output of merging these two sorted arrays using the two pointer technique?
DSA Java
public class MergeSortedArrays { public static int[] merge(int[] a, int[] b) { int[] merged = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] < b[j]) { merged[k++] = a[i++]; } else { merged[k++] = b[j++]; } } while (i < a.length) { merged[k++] = a[i++]; } while (j < b.length) { merged[k++] = b[j++]; } return merged; } public static void main(String[] args) { int[] a = {1, 3, 5}; int[] b = {2, 4, 6}; int[] result = merge(a, b); for (int num : result) { System.out.print(num + " "); } } }
Attempts:
2 left
š” Hint
Two pointer merges by comparing elements from both arrays.
ā Incorrect
The two pointer technique merges sorted arrays by picking smaller elements first, resulting in a sorted merged array.
š§ Conceptual
expertWhy Two Pointer Technique is Not Always Applicable
Which scenario explains why the two pointer technique may not be suitable?
Attempts:
2 left
š” Hint
Think about the requirement for two pointer technique to work.
ā Incorrect
Two pointer technique requires sorted data to move pointers logically. If sorting is impossible, it cannot be applied.
