Challenge - 5 Problems
Two Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of removing duplicates using two pointers
What is the output array after removing duplicates from the sorted array using the two-pointer technique?
DSA Python
def remove_duplicates(nums): if not nums: return 0 i = 0 for j in range(1, len(nums)): if nums[j] != nums[i]: i += 1 nums[i] = nums[j] return nums[:i+1] result = remove_duplicates([1,1,2,2,3,4,4,5]) print(result)
Attempts:
2 left
💡 Hint
Focus on how the two pointers move and overwrite duplicates.
✗ Incorrect
The two-pointer method moves through the array, overwriting duplicates by shifting unique elements forward. The returned slice contains only unique elements.
❓ Predict Output
intermediate2:00remaining
Length of array after removing duplicates
What is the length of the array after removing duplicates using the two-pointer method on this input?
DSA Python
def remove_duplicates(nums): if not nums: return 0 i = 0 for j in range(1, len(nums)): if nums[j] != nums[i]: i += 1 nums[i] = nums[j] return i + 1 length = remove_duplicates([0,0,1,1,1,2,2,3,3,4]) print(length)
Attempts:
2 left
💡 Hint
Count how many unique elements remain after processing.
✗ Incorrect
The unique elements are [0,1,2,3,4], so length is 5.
🔧 Debug
advanced2:00remaining
Identify the error in two-pointer duplicate removal
What error will this code produce when removing duplicates from a sorted array?
DSA Python
def remove_duplicates(nums): i = 0 for j in range(0, len(nums)): if nums[j] == nums[i]: i += 1 nums[i] = nums[j] return nums[:i+1] print(remove_duplicates([1,1,2,3,3]))
Attempts:
2 left
💡 Hint
Check the condition inside the loop and how i is incremented.
✗ Incorrect
The condition is reversed; i increments on duplicates causing i to go out of bounds.
❓ Predict Output
advanced2:00remaining
Final array state after removing duplicates with extra elements
What is the final state of the array after removing duplicates from [2,2,2,3,3,4,5,5,6] using the two-pointer method?
DSA Python
def remove_duplicates(nums): if not nums: return [] i = 0 for j in range(1, len(nums)): if nums[j] != nums[i]: i += 1 nums[i] = nums[j] return nums[:i+1] result = remove_duplicates([2,2,2,3,3,4,5,5,6]) print(result)
Attempts:
2 left
💡 Hint
Only unique elements remain in the returned slice.
✗ Incorrect
Duplicates are overwritten, leaving unique elements [2,3,4,5,6].
🧠 Conceptual
expert2:00remaining
Why does the two-pointer method work for removing duplicates in sorted arrays?
Which explanation best describes why the two-pointer technique correctly removes duplicates from a sorted array?
Attempts:
2 left
💡 Hint
Think about adjacency of duplicates in sorted arrays.
✗ Incorrect
Duplicates appear next to each other in sorted arrays, so one pointer can track the last unique element while the other scans forward to find new unique elements to overwrite duplicates.