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 Duplicate Removal
What is the output array after removing duplicates using the two-pointer method on this sorted array?
DSA C#
int[] nums = {1, 1, 2, 2, 3, 4, 4, 5}; int k = RemoveDuplicates(nums); Console.WriteLine(string.Join(", ", nums[..k])); int RemoveDuplicates(int[] nums) { if (nums.Length == 0) return 0; int i = 0; for (int j = 1; j < nums.Length; j++) { if (nums[j] != nums[i]) { i++; nums[i] = nums[j]; } } return i + 1; }
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 array slice up to the returned length contains only unique values.
🧠 Conceptual
intermediateUnderstanding Two Pointer Roles
In the two-pointer approach to remove duplicates from a sorted array, what is the main role of the pointer 'i'?
Attempts:
2 left
💡 Hint
Think about which pointer moves only when a new unique element is found.
✗ Incorrect
Pointer 'i' marks the index where the next unique element should be placed, effectively building the array of unique elements at the start.
🔧 Debug
advancedIdentify the Bug in Duplicate Removal Code
What error will this code produce when removing duplicates from a sorted array?
DSA C#
int RemoveDuplicates(int[] nums) { int i = 0; for (int j = 1; j <= nums.Length; j++) { if (nums[j] != nums[i]) { i++; nums[i] = nums[j]; } } return i + 1; }
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
✗ Incorrect
The loop uses 'j <= nums.Length' which tries to access an index equal to the array length, causing an IndexOutOfRangeException.
❓ Predict Output
advancedResult Length After Removing Duplicates
What is the returned length after removing duplicates from this sorted array using the two-pointer method?
DSA C#
int[] nums = {0,0,1,1,1,2,2,3,3,4}; int length = RemoveDuplicates(nums); Console.WriteLine(length); int RemoveDuplicates(int[] nums) { if (nums.Length == 0) return 0; int i = 0; for (int j = 1; j < nums.Length; j++) { if (nums[j] != nums[i]) { i++; nums[i] = nums[j]; } } return i + 1; }
Attempts:
2 left
💡 Hint
Count unique numbers in the array.
✗ Incorrect
The unique numbers are 0, 1, 2, 3, 4, so the length returned is 5.
🚀 Application
expertModify Two Pointer to Allow At Most Two Duplicates
How would you modify the two-pointer approach to allow each element to appear at most twice in the sorted array? Which code snippet correctly implements this?
Attempts:
2 left
💡 Hint
Allow duplicates only if the current element is different from the element two positions before.
✗ Incorrect
To allow at most two duplicates, compare current element with the element two positions before. Increment 'i' only if different.
