Challenge - 5 Problems
First and Last Occurrence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind first and last occurrence of 3 in a sorted array
What is the output of this Rust code that finds the first and last occurrence of the number 3 in the array?
DSA Rust
fn first_last_occurrence(arr: &[i32], target: i32) -> (i32, i32) {
let mut first = -1;
let mut last = -1;
for (i, &val) in arr.iter().enumerate() {
if val == target {
if first == -1 {
first = i as i32;
}
last = i as i32;
}
}
(first, last)
}
fn main() {
let arr = [1, 2, 3, 3, 3, 4, 5];
let result = first_last_occurrence(&arr, 3);
println!("({},{})", result.0, result.1);
}Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the function scans from left to right.
✗ Incorrect
The function scans the array and records the first and last index where the target 3 appears. The first 3 is at index 2 and the last at index 4.
❓ Predict Output
intermediateOutput when target is not in the array
What does this Rust code print when searching for 10 in the array?
DSA Rust
fn first_last_occurrence(arr: &[i32], target: i32) -> (i32, i32) {
let mut first = -1;
let mut last = -1;
for (i, &val) in arr.iter().enumerate() {
if val == target {
if first == -1 {
first = i as i32;
}
last = i as i32;
}
}
(first, last)
}
fn main() {
let arr = [1, 2, 3, 4, 5];
let result = first_last_occurrence(&arr, 10);
println!("({},{})", result.0, result.1);
}Attempts:
2 left
💡 Hint
If the target is not found, the function returns (-1, -1).
✗ Incorrect
Since 10 is not in the array, the function never updates first or last from -1.
🔧 Debug
advancedIdentify the error in this Rust code for first and last occurrence
What error does this Rust code produce when compiled?
DSA Rust
fn first_last_occurrence(arr: &[i32], target: i32) -> (i32, i32) {
let mut first = -1;
let mut last = -1;
for i in 0..arr.len() {
if arr[i] = target {
if first == -1 {
first = i as i32;
}
last = i as i32;
}
}
(first, last)
}
fn main() {
let arr = [1, 2, 3, 3, 4];
let result = first_last_occurrence(&arr, 3);
println!("({},{})", result.0, result.1);
}Attempts:
2 left
💡 Hint
Check the if condition syntax for comparison.
✗ Incorrect
The code uses a single = instead of == in the if condition, causing a syntax error.
❓ Predict Output
advancedOutput of binary search approach for first and last occurrence
What is the output of this Rust code that uses binary search to find first and last occurrence of 3?
DSA Rust
fn binary_search_first(arr: &[i32], target: i32) -> i32 {
let mut low = 0;
let mut high = arr.len() as i32 - 1;
let mut result = -1;
while low <= high {
let mid = low + (high - low) / 2;
if arr[mid as usize] == target {
result = mid;
high = mid - 1;
} else if arr[mid as usize] < target {
low = mid + 1;
} else {
high = mid - 1;
}
}
result
}
fn binary_search_last(arr: &[i32], target: i32) -> i32 {
let mut low = 0;
let mut high = arr.len() as i32 - 1;
let mut result = -1;
while low <= high {
let mid = low + (high - low) / 2;
if arr[mid as usize] == target {
result = mid;
low = mid + 1;
} else if arr[mid as usize] < target {
low = mid + 1;
} else {
high = mid - 1;
}
}
result
}
fn main() {
let arr = [1, 2, 3, 3, 3, 4, 5];
let first = binary_search_first(&arr, 3);
let last = binary_search_last(&arr, 3);
println!("({},{})", first, last);
}Attempts:
2 left
💡 Hint
Binary search narrows down the range to find first and last positions.
✗ Incorrect
Binary search finds the first 3 at index 2 and last 3 at index 4 correctly.
🧠 Conceptual
expertWhy binary search is preferred over linear search for first and last occurrence in sorted arrays?
Which of the following is the best reason to use binary search instead of linear search to find first and last occurrence of an element in a sorted array?
Attempts:
2 left
💡 Hint
Think about how many elements each method checks in large arrays.
✗ Incorrect
Binary search reduces the search space by half each step, making it much faster than checking every element.
