Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the left pointer at the start of the array.
DSA Rust
let mut left = [1];Attempts:
3 left
š” Hint
Common Mistakes
Starting left pointer at 1 skips the first element.
Using array length as index causes out-of-bounds error.
ā Incorrect
The left pointer should start at index 0 to begin scanning from the start of the array.
2fill in blank
mediumComplete the code to move the right pointer to the last element of the array.
DSA Rust
let mut right = [1];Attempts:
3 left
š” Hint
Common Mistakes
Setting right pointer to array.len() causes index out of range.
Setting right pointer to 0 starts at the beginning, not the end.
ā Incorrect
The right pointer should start at the last valid index, which is length minus one.
3fill in blank
hardFix the error in the loop condition to continue while left is less than right.
DSA Rust
while [1] < right {
Attempts:
3 left
š” Hint
Common Mistakes
Using right < right causes the loop to never run.
Using left + 1 < right changes the logic incorrectly.
ā Incorrect
The loop should run while the left pointer is less than the right pointer to avoid overlap.
4fill in blank
hardFill both blanks to correctly move pointers based on sum comparison.
DSA Rust
if sum < target { [1] += 1; } else { [2] -= 1; }
Attempts:
3 left
š” Hint
Common Mistakes
Moving sum or target instead of pointers.
Moving pointers in wrong direction.
ā Incorrect
If sum is less than target, move left pointer right to increase sum; else move right pointer left to decrease sum.
5fill in blank
hardFill all three blanks to create a hashmap of numbers and their indices for the hashmap approach.
DSA Rust
let mut map = std::collections::HashMap::new(); for ([1], &num) in nums.iter().enumerate() { if let Some(&idx) = map.get(&([3] - num)) { return vec![idx, [1]]; } map.insert(num, [2]); }
Attempts:
3 left
š” Hint
Common Mistakes
Using wrong variable names for index or target.
Mixing up num and target in map lookup.
ā Incorrect
We use 'i' as the index variable, insert num with index i, and check if target - num exists in map.
