Challenge - 5 Problems
Linear Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of linear search when element is present
What is the output of this Rust code that performs a linear search for the value 7 in the 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 numbers = [3, 5, 7, 9, 11];
match linear_search(&numbers, 7) {
Some(index) => println!("Found at index {}", index),
None => println!("Not found"),
}
}Attempts:
2 left
💡 Hint
Remember that Rust arrays are zero-indexed, so the first element is at index 0.
✗ Incorrect
The linear search checks each element in order. The value 7 is at the third position, which is index 2 (starting from 0). So the output is 'Found at index 2'.
❓ Predict Output
intermediateOutput when element is not in the array
What will this Rust code print when searching for 10 in the 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 numbers = [1, 2, 3, 4, 5];
match linear_search(&numbers, 10) {
Some(index) => println!("Found at index {}", index),
None => println!("Not found"),
}
}Attempts:
2 left
💡 Hint
Check if the target value exists in the array.
✗ Incorrect
The value 10 is not in the array, so the function returns None and the program prints 'Not found'.
🧠 Conceptual
advancedTime complexity of linear search
What is the worst-case time complexity of the linear search algorithm when searching for an element in an array of size n?
Attempts:
2 left
💡 Hint
Consider how many elements might be checked in the worst case.
✗ Incorrect
In the worst case, linear search checks every element once, so it takes O(n) time.
🔧 Debug
advancedIdentify the error in this linear search implementation
What error will this Rust code produce when compiled?
DSA Rust
fn linear_search(arr: &[i32], target: i32) -> Option<usize> {
for index in 0..arr.len() {
if arr[index] = target {
return Some(index);
}
}
None
}Attempts:
2 left
💡 Hint
Check the comparison operator inside the if condition.
✗ Incorrect
The code uses a single equals sign `=` (assignment) instead of `==` (comparison). Assignment returns `()` but `if` expects `bool`, causing mismatched types.
🚀 Application
expertResult of linear search on a large array with duplicates
Given this Rust code searching for 42 in an array with duplicates, what is the output?
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 numbers = [10, 42, 23, 42, 5, 42];
match linear_search(&numbers, 42) {
Some(index) => println!("Found at index {}", index),
None => println!("Not found"),
}
}Attempts:
2 left
💡 Hint
Linear search returns the first occurrence of the target.
✗ Incorrect
The first 42 appears at index 1, so the function returns Some(1) and prints 'Found at index 1'.
