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 of the following Rust code that removes duplicates from a sorted array using two pointers?
DSA Rust
fn remove_duplicates(nums: &mut Vec<i32>) -> usize {
if nums.is_empty() {
return 0;
}
let mut i = 0;
for j in 1..nums.len() {
if nums[j] != nums[i] {
i += 1;
nums[i] = nums[j];
}
}
i + 1
}
fn main() {
let mut nums = vec![1,1,2,2,3,4,4,5];
let len = remove_duplicates(&mut nums);
nums.truncate(len);
println!("{:?}", nums);
}Attempts:
2 left
💡 Hint
Focus on how the two pointers move and how duplicates are overwritten.
✗ Incorrect
The code uses two pointers i and j. i tracks the position of the last unique element. j scans through the array. When nums[j] differs from nums[i], i increments and nums[i] is updated. Finally, the array is truncated to length i+1, removing duplicates.
🧠 Conceptual
intermediateUnderstanding Two Pointer Technique
Why does the two pointer technique work efficiently for removing duplicates from a sorted array?
Attempts:
2 left
💡 Hint
Think about how sorting affects duplicates placement.
✗ Incorrect
Since the array is sorted, duplicates appear next to each other. One pointer keeps track of the last unique element, and the other scans forward to find new unique elements, making the process efficient and in-place.
❓ Predict Output
advancedOutput with All Elements Identical
What will be the output of this Rust code when all elements in the array are identical?
DSA Rust
fn remove_duplicates(nums: &mut Vec<i32>) -> usize {
if nums.is_empty() {
return 0;
}
let mut i = 0;
for j in 1..nums.len() {
if nums[j] != nums[i] {
i += 1;
nums[i] = nums[j];
}
}
i + 1
}
fn main() {
let mut nums = vec![7,7,7,7,7];
let len = remove_duplicates(&mut nums);
nums.truncate(len);
println!("{:?}", nums);
}Attempts:
2 left
💡 Hint
Consider how many unique elements exist.
✗ Incorrect
Since all elements are the same, only one unique element remains after removal. The vector is truncated to length 1, so output is [7].
🔧 Debug
advancedIdentify the Bug in Duplicate Removal Code
What error will this Rust code produce when trying to remove duplicates from a sorted array?
DSA Rust
fn remove_duplicates(nums: &mut Vec<i32>) -> usize {
let mut i = 0;
for j in 1..nums.len() {
if nums[j] != nums[i] {
i += 1;
nums[i] = nums[j];
}
}
i + 1
}
fn main() {
let mut nums = vec![];
let len = remove_duplicates(&mut nums);
nums.truncate(len);
println!("{:?}", nums);
}Attempts:
2 left
💡 Hint
Check what happens when the input vector is empty.
✗ Incorrect
The code lacks an empty vector check. For empty input, the loop (1..0) does not run, but nums[0] is accessed when i=0 and returned as i+1, which is 1. However, since nums is empty, accessing nums[0] in the loop condition or elsewhere causes a panic at index 0 out of bounds.
🚀 Application
expertLength of Array After Removing Duplicates
Given the Rust function below, what is the length of the array after removing duplicates from this input: [0,0,1,1,1,2,2,3,3,4]?
DSA Rust
fn remove_duplicates(nums: &mut Vec<i32>) -> usize {
if nums.is_empty() {
return 0;
}
let mut i = 0;
for j in 1..nums.len() {
if nums[j] != nums[i] {
i += 1;
nums[i] = nums[j];
}
}
i + 1
}Attempts:
2 left
💡 Hint
Count unique elements in the input array.
✗ Incorrect
The unique elements are 0,1,2,3,4 which count to 5. The function returns the count of unique elements after removing duplicates.
