Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the index of the target if found, else return None.
DSA Rust
fn linear_search(arr: &[i32], target: i32) -> Option<usize> {
for (index, &value) in arr.iter().enumerate() {
if value == [1] {
return Some(index);
}
}
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' instead of 'target' in the comparison.
Comparing 'value' to 'arr' which is the whole array.
✗ Incorrect
We compare each element 'value' with the 'target' to find a match.
2fill in blank
mediumComplete the code to return None if the target is not found after checking all elements.
DSA Rust
fn linear_search(arr: &[i32], target: i32) -> Option<usize> {
for (index, &value) in arr.iter().enumerate() {
if value == target {
return Some(index);
}
}
[1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning Some(0) which incorrectly indicates the target is at index 0.
Returning 0 which is not an Option type.
✗ Incorrect
If the target is not found, the function should return None to indicate absence.
3fill in blank
hardFix the error in the loop to correctly iterate over the array with index and value.
DSA Rust
fn linear_search(arr: &[i32], target: i32) -> Option<usize> {
for ([1], &value) in arr.iter().enumerate() {
if value == target {
return Some(index);
}
}
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the loop but returning 'index'.
Using 'value' as the index variable.
✗ Incorrect
The variable 'index' is used later to return the position, so it must be named correctly in the loop.
4fill in blank
hardFill both blanks to complete the function that searches for target and returns its index or None.
DSA Rust
fn linear_search(arr: &[i32], target: i32) -> Option<usize> {
for ([1], &value) in arr.iter().enumerate() {
if value [2] target {
return Some(index);
}
}
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causing wrong logic.
Using a different variable name than 'index' causing errors.
✗ Incorrect
The loop variable must be 'index' and the comparison operator must be '==' to check equality.
5fill in blank
hardFill all three blanks to create a linear search that returns the index of the target or None.
DSA Rust
fn linear_search(arr: &[i32], target: i32) -> Option<usize> {
for ([1], &[2]) in arr.iter().enumerate() {
if [3] == target {
return Some(index);
}
}
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using 'idx' instead of 'index' causing errors.
✗ Incorrect
The index variable is 'index', the value variable is 'value', and the comparison uses 'value'.
