Challenge - 5 Problems
Two Pointer Master
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 this code that checks if any two numbers in the sorted array sum to 10?
DSA Python
def two_sum(arr, target): left, right = 0, len(arr) - 1 while left < right: current_sum = arr[left] + arr[right] if current_sum == target: return True elif current_sum < target: left += 1 else: right -= 1 return False print(two_sum([1, 2, 4, 5, 6, 8], 10))
Attempts:
2 left
💡 Hint
Think about pairs that add up to 10 in the array.
✗ Incorrect
The function uses two pointers starting at the ends of the sorted array. It finds 2 and 8 which sum to 10, so returns True.
❓ Predict Output
intermediate2:00remaining
Result of Merging Two Sorted Arrays
What is the printed output after merging two sorted arrays using two pointers?
DSA Python
def merge_sorted(arr1, arr2): i, j = 0, 0 merged = [] while i < len(arr1) and j < len(arr2): if arr1[i] < arr2[j]: merged.append(arr1[i]) i += 1 else: merged.append(arr2[j]) j += 1 merged.extend(arr1[i:]) merged.extend(arr2[j:]) return merged print(merge_sorted([1, 3, 5], [2, 4, 6]))
Attempts:
2 left
💡 Hint
Two pointers pick the smaller element from each array step by step.
✗ Incorrect
The function merges two sorted arrays into one sorted array by comparing elements at pointers and appending the smaller one.
❓ Predict Output
advanced2:00remaining
Output of Removing Duplicates from Sorted Array
What is the output list after removing duplicates from a sorted array using two pointers?
DSA Python
def remove_duplicates(nums): if not nums: return [] write = 1 for read in range(1, len(nums)): if nums[read] != nums[read - 1]: nums[write] = nums[read] write += 1 return nums[:write] print(remove_duplicates([1, 1, 2, 3, 3, 4, 5, 5]))
Attempts:
2 left
💡 Hint
Two pointers track unique elements and overwrite duplicates.
✗ Incorrect
The write pointer moves only when a new unique element is found, resulting in a list without duplicates.
❓ Predict Output
advanced2:00remaining
Output of Partitioning Array by Pivot
What is the output array after partitioning around pivot 5 using two pointers?
DSA Python
def partition(arr, pivot): left, right = 0, len(arr) - 1 while left <= right: while left <= right and arr[left] < pivot: left += 1 while left <= right and arr[right] >= pivot: right -= 1 if left < right: arr[left], arr[right] = arr[right], arr[left] left += 1 right -= 1 return arr print(partition([7, 2, 5, 3, 8, 1, 6], 5))
Attempts:
2 left
💡 Hint
Elements less than pivot move left, others right.
✗ Incorrect
The function swaps elements to ensure all less than pivot are left side, others right side, order inside groups may vary.
🧠 Conceptual
expert2:00remaining
Number of Unique Pairs with Given Sum
Given a sorted array, how many unique pairs of numbers sum to 7 using the two pointer technique?
DSA Python
def count_pairs(arr, target): left, right = 0, len(arr) - 1 count = 0 while left < right: current_sum = arr[left] + arr[right] if current_sum == target: count += 1 left += 1 right -= 1 while left < right and arr[left] == arr[left - 1]: left += 1 while left < right and arr[right] == arr[right + 1]: right -= 1 elif current_sum < target: left += 1 else: right -= 1 return count print(count_pairs([1, 2, 2, 3, 4, 4, 5], 7))
Attempts:
2 left
💡 Hint
Count pairs without counting duplicates multiple times.
✗ Incorrect
Pairs are (2,5) and (3,4). The duplicates 2 and 4 are counted once each, so total 2 unique pairs.