Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first occurrence index of target in the sorted array.
DSA Rust
fn first_occurrence(nums: &[i32], target: i32) -> i32 {
let mut left = 0;
let mut right = nums.len() as i32 - 1;
let mut result = -1;
while left <= right {
let mid = left + (right - left) / 2;
if nums[mid as usize] == target {
result = mid;
right = mid[1]1;
} else if nums[mid as usize] < target {
left = mid + 1;
} else {
right = mid - 1;
}
}
result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using right = mid + 1 moves right pointer in wrong direction.
Not updating result when target is found.
✗ Incorrect
To find the first occurrence, when we find the target, we move the right pointer to mid - 1 to continue searching left side.
2fill in blank
mediumComplete the code to find the last occurrence index of target in the sorted array.
DSA Rust
fn last_occurrence(nums: &[i32], target: i32) -> i32 {
let mut left = 0;
let mut right = nums.len() as i32 - 1;
let mut result = -1;
while left <= right {
let mid = left + (right - left) / 2;
if nums[mid as usize] == target {
result = mid;
left = mid[1]1;
} else if nums[mid as usize] < target {
left = mid + 1;
} else {
right = mid - 1;
}
}
result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using left = mid - 1 moves left pointer in wrong direction.
Not updating result when target is found.
✗ Incorrect
To find the last occurrence, when target is found, move the left pointer to mid + 1 to search right side.
3fill in blank
hardFix the error in the code to correctly count occurrences of target in the sorted array.
DSA Rust
fn count_occurrences(nums: &[i32], target: i32) -> i32 {
let first = first_occurrence(nums, target);
if first == -1 {
return 0;
}
let last = last_occurrence(nums, target);
last - first[1]1
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add 1 to the difference.
Subtracting 1 instead of adding.
✗ Incorrect
The count is last index minus first index plus one because indices are zero-based.
4fill in blank
hardFill both blanks to create a dictionary of word lengths for words longer than 3 characters.
DSA Rust
fn word_lengths(words: &[&str]) -> std::collections::HashMap<String, usize> {
words.iter()
.filter(|&&word| word.len() [1] 3)
.map(|&word| (word.to_string(), word[2]()))
.collect()
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter.
Using 'chars' method instead of 'len' for length.
✗ Incorrect
Filter words with length greater than 3 and map to (word, length) pairs.
5fill in blank
hardFill all three blanks to create a dictionary of uppercase keys and values greater than zero.
DSA Rust
fn filter_and_uppercase(data: &std::collections::HashMap<String, i32>) -> std::collections::HashMap<String, i32> {
data.iter()
.filter(|(_, &v)| v [1] 0)
.map(|(k, &v)| (k.[2](), v))
.collect::<std::collections::HashMap<_, _>>()
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter.
Using 'to_lowercase' instead of 'to_uppercase'.
✗ Incorrect
Filter values greater than 0 and convert keys to uppercase.
