What is the main difference between Binary Search and Linear Search?
Binary Search divides the search space in half each step and requires a sorted list, making it faster. Linear Search checks each element one by one and works on any list.
Click to reveal answer
beginner
Why does Binary Search require a sorted list?
Because it decides which half to search next based on comparing the middle element to the target, so the order must be known.
Click to reveal answer
intermediate
What is the time complexity of Linear Search and Binary Search?
Linear Search has O(n) time complexity (checks each element). Binary Search has O(log n) time complexity (halves search space each step).
Click to reveal answer
intermediate
In what situation might Linear Search be faster than Binary Search?
If the list is very small or unsorted, Linear Search can be faster because Binary Search requires sorting first or only works on sorted lists.
Click to reveal answer
intermediate
What is the real cost difference between Binary Search and Linear Search in terms of comparisons?
Binary Search uses fewer comparisons, about log₂(n), while Linear Search may check up to n elements, making Binary Search more efficient for large sorted lists.
Click to reveal answer
Which search algorithm requires the list to be sorted?
ABinary Search
BLinear Search
CBoth
DNeither
✗ Incorrect
Binary Search only works correctly on sorted lists because it decides which half to search based on order.
What is the worst-case time complexity of Linear Search?
AO(n)
BO(1)
CO(n log n)
DO(log n)
✗ Incorrect
Linear Search may check every element, so its worst-case time is O(n).
How many comparisons does Binary Search make in the worst case for a list of size 16?
A16
B8
C4
D1
✗ Incorrect
Binary Search makes about log₂(16) = 4 comparisons in the worst case.
When might Linear Search be preferred over Binary Search?
AWhen the list is sorted
BNever
CWhen the list is very large
DWhen the list is unsorted or very small
✗ Incorrect
Linear Search works on unsorted or small lists without extra preparation.
Which search algorithm is generally more efficient for large sorted lists?
ALinear Search
BBinary Search
CBoth are equal
DDepends on the data
✗ Incorrect
Binary Search is more efficient for large sorted lists due to fewer comparisons.
Explain the key differences in how Binary Search and Linear Search work and their real cost differences.
Think about how each algorithm moves through the list and how many checks they do.
You got /6 concepts.
Describe a scenario where Linear Search might be better than Binary Search despite Binary Search being faster in theory.
Consider the cost of sorting and list size.
You got /4 concepts.
Practice
(1/5)
1. Which search method is generally faster on a sorted list of 1,000,000 elements?
easy
A. Depends on the element's position only
B. Linear Search
C. Both are equally fast
D. Binary Search
Solution
Step 1: Understand search methods on sorted lists
Binary Search divides the list repeatedly, reducing search space quickly. Linear Search checks elements one by one.
Step 2: Compare time complexity for large lists
Binary Search runs in O(log n) time, which is much faster than Linear Search's O(n) for large n like 1,000,000.
Final Answer:
Binary Search -> Option D
Quick Check:
Binary Search faster on sorted large lists [OK]
Hint: Binary Search halves search space each step [OK]
Common Mistakes:
Thinking Linear Search is faster for sorted lists
Ignoring list size impact
Confusing sorted vs unsorted list performance
2. Which Rust code snippet correctly implements a linear search for a value in a slice?
easy
A. fn linear_search(arr: &[i32], target: i32) -> Option<usize> { let mut i = 0; while i < arr.len() { if arr[i] == target { return Some(i); } i += 1; } None }
B. fn linear_search(arr: &[i32], target: i32) -> Option<usize> { for i in 0..arr.len()-1 { if arr[i] == target { return Some(i); } } None }
C. fn linear_search(arr: &[i32], target: i32) -> usize { for i in 0..arr.len() { if arr[i] == target { return i; } } 0 }
D. fn linear_search(arr: &[i32], target: i32) -> Option<usize> { for i in 1..arr.len() { if arr[i] == target { return Some(i); } } None }
Solution
Step 1: Check function signature and return type
Correct implementations should return Option<usize>, idiomatic for search functions. Returning plain usize with 0 on not found is ambiguous since index 0 is valid.
Step 2: Verify loop correctness and indexing
The for loop from 0 to arr.len()-1 misses the last element. The while loop with mutable i from 0 while i < arr.len() covers all indices correctly. The for loop from 1 skips index 0.
Final Answer:
While loop version -> Option A
Quick Check:
Correct linear search loop and return [OK]
Hint: Check loop covers all indices and returns Option properly [OK]
Common Mistakes:
Skipping first element in loop
Returning ambiguous index instead of Option
Incorrect loop bounds
3. What is the output of this Rust code using binary search on a sorted array?
let arr = [2, 4, 6, 8, 10];
let target = 6;
let result = arr.binary_search(&target);
println!("{:?}", result);
medium
A. Err(2)
B. Ok(2)
C. Ok(3)
D. Err(3)
Solution
Step 1: Understand binary_search return type
Rust's binary_search returns Ok(index) if found, Err(insert_pos) if not found.
Step 2: Find target index in array
Array is [2,4,6,8,10], target 6 is at index 2 (0-based).
Final Answer:
Ok(2) -> Option B
Quick Check:
binary_search returns Ok(index) when found [OK]
Hint: binary_search returns Ok(index) if found [OK]
Common Mistakes:
Confusing Ok and Err results
Miscounting zero-based index
Assuming Err means found
4. Identify the bug in this Rust binary search implementation and select the fix:
fn binary_search(arr: &[i32], target: i32) -> Option<usize> {
let mut low = 0;
let mut high = arr.len();
while low <= high {
let mid = (low + high) / 2;
if arr[mid] == target {
return Some(mid);
} else if arr[mid] < target {
low = mid + 1;
} else {
high = mid - 1;
}
}
None
}
medium
A. Change high initialization to arr.len() - 1
B. Change while condition to low > high
C. Change mid calculation to low + (high - low) / 2
D. No bug, code is correct
Solution
Step 1: Analyze high variable initialization
High is set to arr.len(), which is out of bounds since valid indices are 0 to arr.len()-1.
Step 2: Fix high initialization
Set high to arr.len() - 1 to avoid index out of bounds error when accessing arr[mid].
Final Answer:
Change high initialization to arr.len() - 1 -> Option A
Quick Check:
High must be last valid index [OK]
Hint: High index must be last valid array index [OK]
Common Mistakes:
Using arr.len() as high causes out of bounds
Changing while condition incorrectly
Ignoring mid calculation overflow
5. You have a sorted list of 10,000 elements and want to find if a value exists. Which approach minimizes average search steps considering the cost of comparisons and index calculations?
Assume: - Each comparison costs 1 unit - Each index calculation (like mid) costs 2 units - Linear search does only comparisons - Binary search does comparisons + index calculations per step
hard
A. Linear search is cheaper because it avoids index calculations
B. Both have equal cost on average
C. Binary search is cheaper because it reduces comparisons drastically despite index costs
D. Linear search is cheaper only if the element is near the start
Solution
Step 1: Calculate linear search average cost
Linear search average steps = n/2 = 5000 comparisons, cost = 5000 * 1 = 5000 units.