Challenge - 5 Problems
Two Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of removing duplicates from sorted array
What is the output array after removing duplicates using the two-pointer method on this sorted array?
DSA Go
func removeDuplicates(nums []int) []int { if len(nums) == 0 { return nums } i := 0 for j := 1; j < len(nums); j++ { if nums[j] != nums[i] { i++ nums[i] = nums[j] } } return nums[:i+1] } input := []int{1,1,2,2,3,4,4,5} output := removeDuplicates(input) fmt.Println(output)
Attempts:
2 left
💡 Hint
Focus on how the two pointers move and overwrite duplicates.
✗ Incorrect
The two-pointer method moves through the array, copying unique elements forward. The final slice includes only unique elements.
❓ Predict Output
intermediateLength of array after removing duplicates
What is the length of the array after removing duplicates from this sorted array using the two-pointer method?
DSA Go
func removeDuplicates(nums []int) int { if len(nums) == 0 { return 0 } i := 0 for j := 1; j < len(nums); j++ { if nums[j] != nums[i] { i++ nums[i] = nums[j] } } return i + 1 } input := []int{0,0,1,1,1,2,2,3,3,4} length := removeDuplicates(input) fmt.Println(length)
Attempts:
2 left
💡 Hint
Count unique elements after removing duplicates.
✗ Incorrect
The unique elements are 0,1,2,3,4 so length is 5.
🔧 Debug
advancedIdentify the error in two-pointer duplicate removal code
Which option contains a bug that causes incorrect removal of duplicates in a sorted array?
DSA Go
func removeDuplicates(nums []int) int { if len(nums) == 0 { return 0 } i := 0 for j := 1; j < len(nums); j++ { if nums[j] == nums[i] { i++ nums[i] = nums[j] } } return i + 1 }
Attempts:
2 left
💡 Hint
Check the condition that decides when to move the pointer and copy values.
✗ Incorrect
The condition must check for inequality to detect new unique elements. Using equality causes wrong behavior.
🧠 Conceptual
advancedWhy does the two-pointer method work for removing duplicates in a sorted array?
Choose the best explanation for why the two-pointer technique correctly removes duplicates from a sorted array.
Attempts:
2 left
💡 Hint
Think about how sorting affects the position of duplicates.
✗ Incorrect
Duplicates appear next to each other in a sorted array, so one pointer can track the last unique element and the other scans forward to find new unique elements.
❓ Predict Output
expertOutput after removing duplicates with multiple duplicates present
What is the output slice after running the two-pointer duplicate removal function on this input?
DSA Go
func removeDuplicates(nums []int) []int { if len(nums) == 0 { return nums } i := 0 for j := 1; j < len(nums); j++ { if nums[j] != nums[i] { i++ nums[i] = nums[j] } } return nums[:i+1] } input := []int{2,2,2,3,3,4,4,4,4,5,5,6} output := removeDuplicates(input) fmt.Println(output)
Attempts:
2 left
💡 Hint
Check how many unique numbers are in the input and how the function slices the array.
✗ Incorrect
The unique numbers are 2,3,4,5,6 so the output slice contains these five elements.
