Challenge - 5 Problems
Count Occurrences Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of counting occurrences in a sorted array
What is the output of the following Rust code that counts occurrences of the number 3 in a sorted array?
DSA Rust
fn count_occurrences(arr: &[i32], target: i32) -> usize {
let first = arr.binary_search(&target).ok();
if first.is_none() {
return 0;
}
let first_index = first.unwrap();
let mut count = 1;
let mut i = first_index + 1;
while i < arr.len() && arr[i] == target {
count += 1;
i += 1;
}
count
}
fn main() {
let arr = [1, 2, 3, 3, 3, 4, 5];
let result = count_occurrences(&arr, 3);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Think about how many times 3 appears consecutively in the array.
✗ Incorrect
The binary_search finds an occurrence of 3 at index 2. Then the loop counts how many times 3 appears after that index. Since 3 appears at indices 2, 3, and 4, the output is 3.
❓ Predict Output
intermediateOutput when element is not present
What will be printed when counting occurrences of 6 in the sorted array?
DSA Rust
fn count_occurrences(arr: &[i32], target: i32) -> usize {
let first = arr.binary_search(&target).ok();
if first.is_none() {
return 0;
}
let first_index = first.unwrap();
let mut count = 1;
let mut i = first_index + 1;
while i < arr.len() && arr[i] == target {
count += 1;
i += 1;
}
count
}
fn main() {
let arr = [1, 2, 3, 3, 3, 4, 5];
let result = count_occurrences(&arr, 6);
println!("{}", result);
}Attempts:
2 left
💡 Hint
If the element is not found, what does the function return?
✗ Incorrect
binary_search returns None for elements not found, so the function returns 0 indicating no occurrences.
🧠 Conceptual
advancedWhy use binary search for counting occurrences?
Why is binary search a good choice for counting occurrences of an element in a sorted array?
Attempts:
2 left
💡 Hint
Think about the speed of searching in sorted arrays.
✗ Incorrect
Binary search quickly finds the target element's position in O(log n) time, which is faster than scanning the entire array.
❓ Predict Output
advancedOutput of counting occurrences with duplicates at start and end
What is the output of counting occurrences of 2 in this sorted array?
DSA Rust
fn count_occurrences(arr: &[i32], target: i32) -> usize {
let first = arr.binary_search(&target).ok();
if first.is_none() {
return 0;
}
let first_index = first.unwrap();
let mut count = 1;
let mut i = first_index + 1;
while i < arr.len() && arr[i] == target {
count += 1;
i += 1;
}
count
}
fn main() {
let arr = [2, 2, 2, 3, 4, 5, 6, 2];
let result = count_occurrences(&arr, 2);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Remember the array is sorted except the last element.
✗ Incorrect
binary_search finds a 2 at index 2. The loop counts how many times 2 appears after that index. Since arr[3] == 3 != 2, the output is 1. The last 2 at index 7 is not counted because it is not consecutive.
🔧 Debug
expertIdentify the bug in counting occurrences code
What is the bug in this Rust code for counting occurrences of an element in a sorted array?
DSA Rust
fn count_occurrences(arr: &[i32], target: i32) -> usize {
let mut count = 0;
for &item in arr {
if item == target {
count += 1;
} else if count > 0 {
break;
}
}
count
}
fn main() {
let arr = [1, 2, 2, 2, 3, 4, 5];
let result = count_occurrences(&arr, 2);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Consider the time complexity and the use of binary search.
✗ Incorrect
The code scans the array linearly and breaks after counting consecutive occurrences, which works but is less efficient than binary search for large sorted arrays.
