Why is Linear Search not efficient for large sorted lists?
Because it checks every element one by one, ignoring the order, so it can be slow compared to faster methods like binary search.
Click to reveal answer
beginner
What does the function return if the target is not found in Linear Search?
It returns None, meaning the target is not present in the list.
Click to reveal answer
What does Linear Search do when it finds the target element?
ASorts the list
BContinues searching the rest of the list
CDeletes the target element
DReturns the index of the target
✗ Incorrect
Linear Search stops and returns the index as soon as it finds the target.
What is the best case time complexity of Linear Search?
AO(n)
BO(1)
CO(log n)
DO(n^2)
✗ Incorrect
If the target is the first element, Linear Search finds it immediately in constant time O(1).
Which data structure is Linear Search usually applied to?
AArrays or lists
BBinary trees
CGraphs
DHash tables
✗ Incorrect
Linear Search works on arrays or lists by checking elements one by one.
If the list has 1000 elements and the target is not present, how many checks does Linear Search do?
A0
B1
C1000
D500
✗ Incorrect
In the worst case, Linear Search checks every element, so 1000 checks.
Which of these is a disadvantage of Linear Search?
ASlow for large lists
BNeeds sorted data
CUses extra memory
DOnly works on numbers
✗ Incorrect
Linear Search is slow for large lists because it checks each element one by one.
Explain how Linear Search works step-by-step on a list of numbers.
Think about looking for a book on a shelf one by one.
You got /4 concepts.
Describe the advantages and disadvantages of using Linear Search.
Consider when it is easy and when it is slow.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Linear Search Algorithm?
easy
A. Divide the list into halves repeatedly to find the target
B. Use a hash table to find the target instantly
C. Sort the list first then search using binary steps
D. Check each element one by one until the target is found or list ends
Solution
Step 1: Understand linear search process
Linear search checks each element in order from start to end.
Step 2: Compare with other search methods
Binary search divides list, hash uses extra memory, linear is simple sequential check.
Final Answer:
Check each element one by one until the target is found or list ends -> Option D
Quick Check:
Linear search = sequential check [OK]
Hint: Linear search means checking items one by one [OK]
Common Mistakes:
Confusing linear search with binary search
Thinking linear search requires sorted data
Assuming linear search uses extra memory
2. Which of the following is the correct Rust syntax to start a linear search loop over a vector arr?
easy
A. while i < arr.len() { }
B. for i in arr { }
C. for i in 0..arr.len() { }
D. loop i in arr.len() { }
Solution
Step 1: Identify correct Rust for loop syntax
Rust uses for i in 0..arr.len() to loop over indices.
Step 2: Check other options for syntax errors
for i in arr iterates over values, not indices; while needs initialization; loop i in is invalid.
Final Answer:
for i in 0..arr.len() { } -> Option C
Quick Check:
Rust for loop over range = for i in 0..len() [OK]
Hint: Use for i in 0..arr.len() to loop indices [OK]
Common Mistakes:
Using for i in arr to get indices instead of values
Writing invalid loop syntax like loop i in ...
Forgetting to initialize loop variable in while loops
3. What will be the output of this Rust code snippet?
let arr = vec![3, 5, 7, 9];
let target = 7;
let mut index = -1;
for i in 0..arr.len() {
if arr[i] == target {
index = i as i32;
break;
}
}
println!("{}", index);
Hint: Stop loop at first match, print that index [OK]
Common Mistakes:
Not breaking loop after finding target
Printing index before loop ends
Confusing index type casting
4. Identify the error in this Rust linear search code and choose the fix:
let arr = vec![1, 2, 3];
let target = 4;
let mut found = false;
for i in 0..arr.len() {
if arr[i] = target {
found = true;
break;
}
}
println!("{}", found);
medium
A. Use '==' for comparison instead of '='
B. Change 'found' to integer type
C. Remove the break statement
D. Use while loop instead of for loop
Solution
Step 1: Check the if condition syntax
The code uses '=' which is assignment, not comparison. It should be '==' to compare values.
Step 2: Verify other options
Changing 'found' type or removing break won't fix syntax error. Using while loop is valid but not necessary.
Final Answer:
Use '==' for comparison instead of '=' -> Option A
Quick Check:
Comparison needs '==' not '=' [OK]
Hint: Use '==' to compare values, '=' assigns [OK]
Common Mistakes:
Using '=' instead of '==' in if conditions
Removing break causing unnecessary iterations
Changing variable types unnecessarily
5. You want to find all indices of a target value in a vector arr using linear search in Rust. Which approach correctly collects all matching indices?
hard
A. Use a loop over indices, push index to a vector when element equals target
B. Break the loop at first match and return that index
C. Sort the vector first, then use binary search for all matches
D. Use a hash map to store element counts and find indices
Solution
Step 1: Understand requirement to find all indices
We must check every element and collect indices where element equals target.
Step 2: Evaluate options for correctness
Breaking loop stops early, sorting changes order, hash map counts but doesn't store indices. Only pushing indices in loop works.
Final Answer:
Use a loop over indices, push index to a vector when element equals target -> Option A
Quick Check:
Collect all matches by looping and storing indices [OK]
Hint: Collect indices by checking all elements without breaking [OK]
Common Mistakes:
Breaking loop too early to find all matches
Sorting vector which changes original order
Using hash map counts instead of indices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.