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 Go code that uses two pointers to check if any two numbers sum to 10?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 4, 4, 5, 6} left, right := 0, len(arr)-1 found := false for left < right { sum := arr[left] + arr[right] if sum == 10 { found = true break } else if sum < 10 { left++ } else { right-- } } fmt.Println(found) }
Attempts:
2 left
💡 Hint
Think about how the two pointers move to find pairs that sum to 10.
✗ Incorrect
The two pointers start at the ends of the sorted array. They move inward adjusting based on the sum. The pair (4,6) sums to 10, so found becomes true.
❓ Predict Output
intermediateResulting Array After Removing Duplicates
What is the output of this Go code that uses two pointers to remove duplicates from a sorted array and prints the resulting array slice?
DSA Go
package main import "fmt" func main() { arr := []int{1, 1, 2, 2, 3, 4, 4, 5} if len(arr) == 0 { fmt.Println([]int{}) return } j := 0 for i := 1; i < len(arr); i++ { if arr[i] != arr[j] { j++ arr[j] = arr[i] } } fmt.Println(arr[:j+1]) }
Attempts:
2 left
💡 Hint
The code keeps unique elements at the front and slices the array accordingly.
✗ Incorrect
The two pointers track unique elements. The final slice contains only unique values: 1, 2, 3, 4, 5.
🔧 Debug
advancedIdentify the Error in Two Pointer Partition Code
What error does this Go code produce when trying to partition an array around a pivot using two pointers?
DSA Go
package main import "fmt" func main() { arr := []int{9, 12, 3, 5, 14, 10, 10} pivot := 10 left, right := 0, len(arr)-1 for left <= right { for arr[left] < pivot { left++ } for arr[right] > pivot { right-- } if left <= right { arr[left], arr[right] = arr[right], arr[left] left++ right-- } } fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Check the inner loops and pointer increments carefully.
✗ Incorrect
If arr[left] == pivot, the inner loop for arr[left] < pivot does not advance left, causing left to stay and the outer loop to repeat endlessly.
❓ Predict Output
advancedFind Maximum Water Container Area
Given an array representing heights of vertical lines, which option correctly computes the maximum water container area using two pointers?
DSA Go
package main import "fmt" func maxArea(height []int) int { left, right := 0, len(height)-1 maxArea := 0 for left < right { width := right - left var minHeight int if height[left] < height[right] { minHeight = height[left] } else { minHeight = height[right] } area := width * minHeight if area > maxArea { maxArea = area } if height[left] < height[right] { left++ } else { right-- } } return maxArea } func main() { fmt.Println(maxArea([]int{1,8,6,2,5,4,8,3,7})) }
Attempts:
2 left
💡 Hint
The maximum area is between lines at index 1 and 8.
✗ Incorrect
The maximum area is 49, formed between height 8 at index 1 and height 7 at index 8 with width 7.
🧠 Conceptual
expertTwo Pointer Technique Time Complexity
What is the time complexity of the two pointer technique when used to find pairs in a sorted array that sum to a target value?
Attempts:
2 left
💡 Hint
Consider how many times each pointer moves.
✗ Incorrect
Each pointer moves at most n times, so total operations are proportional to n, making it O(n).
