Challenge - 5 Problems
Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Linear Search on a Sorted Array
What is the output of the Rust code performing a linear search for the value 7 in the given sorted array?
DSA Rust
fn linear_search(arr: &[i32], target: i32) -> Option<usize> {
for (index, &value) in arr.iter().enumerate() {
if value == target {
return Some(index);
}
}
None
}
fn main() {
let arr = [1, 3, 5, 7, 9, 11];
match linear_search(&arr, 7) {
Some(index) => println!("Found at index {}", index),
None => println!("Not found"),
}
}Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and linear search checks elements one by one.
✗ Incorrect
The linear search checks each element from the start. The value 7 is at index 3 (0-based).
❓ Predict Output
intermediateOutput of Binary Search on a Sorted Array
What is the output of the Rust code performing a binary search for the value 7 in the given sorted array?
DSA Rust
fn binary_search(arr: &[i32], target: i32) -> Option<usize> {
let mut low = 0;
let mut high = arr.len() - 1;
while low <= high {
let mid = (low + high) / 2;
if arr[mid] == target {
return Some(mid);
} else if arr[mid] < target {
low = mid + 1;
} else {
if mid == 0 { break; }
high = mid - 1;
}
}
None
}
fn main() {
let arr = [1, 3, 5, 7, 9, 11];
match binary_search(&arr, 7) {
Some(index) => println!("Found at index {}", index),
None => println!("Not found"),
}
}Attempts:
2 left
💡 Hint
Binary search divides the array and checks the middle element each time.
✗ Incorrect
The binary search finds 7 at index 3 by checking the middle elements and narrowing the search range.
🧠 Conceptual
advancedTime Complexity Difference Between Linear and Binary Search
Which statement correctly describes the time complexity difference between linear search and binary search on a sorted array of size n?
Attempts:
2 left
💡 Hint
Think about how many elements each search checks in the worst case.
✗ Incorrect
Linear search checks each element one by one, so it takes O(n) time. Binary search halves the search space each time, so it takes O(log n) time.
❓ Predict Output
advancedOutput of Binary Search on Unsorted Array
What is the output of the Rust binary search code when searching for 7 in an unsorted array?
DSA Rust
fn binary_search(arr: &[i32], target: i32) -> Option<usize> {
let mut low = 0;
let mut high = arr.len() - 1;
while low <= high {
let mid = (low + high) / 2;
if arr[mid] == target {
return Some(mid);
} else if arr[mid] < target {
low = mid + 1;
} else {
if mid == 0 { break; }
high = mid - 1;
}
}
None
}
fn main() {
let arr = [9, 1, 11, 3, 7, 5];
match binary_search(&arr, 7) {
Some(index) => println!("Found at index {}", index),
None => println!("Not found"),
}
}Attempts:
2 left
💡 Hint
Binary search requires the array to be sorted to work correctly.
✗ Incorrect
Binary search assumes the array is sorted. On an unsorted array, it may not find the target even if it exists.
🧠 Conceptual
expertReal Cost Difference in Practice Between Linear and Binary Search
Which statement best explains the real cost difference between linear search and binary search when searching for an element in a large sorted array stored in memory?
Attempts:
2 left
💡 Hint
Consider how memory access patterns affect speed, not just the number of comparisons.
✗ Incorrect
Binary search uses fewer comparisons but jumps around memory, causing cache misses. Linear search accesses memory sequentially, which can be faster for small arrays or cached data.
