What is the main difference between Binary Search and Linear Search?
Binary Search divides the search space in half each time, working only on sorted data, making it faster. Linear Search checks each element one by one, working on any list but slower for large data.
Click to reveal answer
beginner
Why does Binary Search require the list to be sorted?
Binary Search compares the target with the middle element to decide which half to search next. This only works if the list is sorted, so the halves are meaningful.
Click to reveal answer
intermediate
What is the time complexity of Linear Search and Binary Search?
Linear Search has time complexity O(n), meaning it may check every element. Binary Search has time complexity O(log n), meaning it reduces the search space quickly by half 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, which takes extra time.
Click to reveal answer
intermediate
Explain the real cost difference between Binary Search and Linear Search in terms of operations.
Binary Search uses fewer comparisons by halving the search space each time, so it performs about log2(n) comparisons. Linear Search may check every element, up to n comparisons, making it costlier for large 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 log n)
BO(log n)
CO(n)
DO(1)
✗ Incorrect
Linear Search may check every element, so its worst-case time is O(n).
How many comparisons does Binary Search roughly perform on a list of size 16?
A4
B8
C16
D1
✗ Incorrect
Binary Search performs about log2(16) = 4 comparisons.
When is Linear Search preferred over Binary Search?
AWhen the list is very large
BWhen the list is unsorted or very small
CWhen the list is sorted
DNever
✗ Incorrect
Linear Search works on unsorted or small lists without extra sorting cost.
Which search algorithm halves the search space each step?
ALinear Search
BNeither
CBoth
DBinary Search
✗ Incorrect
Binary Search divides the search space in half each step to find the target faster.
Describe how Binary Search and Linear Search differ in their approach and when each is best used.
Think about sorting and how the search space changes.
You got /6 concepts.
Explain the real cost difference between Binary Search and Linear Search in terms of number of comparisons and time complexity.
Focus on how many elements each algorithm checks.
You got /5 concepts.
Practice
(1/5)
1. Which search algorithm is generally faster on a sorted list of 1,000,000 elements?
easy
A. Binary Search
B. Linear Search
C. Both are equally fast
D. Neither works on sorted lists
Solution
Step 1: Understand the search algorithms
Binary Search divides the list in half repeatedly, while Linear Search checks each element one by one.
Step 2: Compare efficiency on large sorted lists
Binary Search takes about log2(1,000,000) ≈ 20 steps, Linear Search takes up to 1,000,000 steps.
Final Answer:
Binary Search -> Option A
Quick Check:
Binary Search is faster on sorted lists [OK]
Hint: Binary Search halves search space; Linear checks all [OK]
Common Mistakes:
Thinking Linear Search is faster on sorted lists
Confusing sorted and unsorted list requirements
Ignoring the difference in number of steps
2. Which of the following is the correct syntax to start a binary search on a sorted list named arr in Python?
easy
A. arr.binary_search(target)
B. bisect.bisect_left(arr, target)
C. binary_search(arr, target)
D. search(arr, target)
Solution
Step 1: Recall Python's binary search method
Python's standard library uses the bisect module for binary search operations.
Step 2: Identify correct function call
bisect.bisect_left(arr, target) returns the insertion point using binary search.
Final Answer:
bisect.bisect_left(arr, target) -> Option B
Quick Check:
Use bisect module for binary search in Python [OK]
Hint: Use bisect module for binary search in Python [OK]
Common Mistakes:
Assuming binary_search is a built-in function
Trying to call binary_search as a method on list
Using generic search functions without binary search
3. What is the output of the following code snippet?
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
arr = [2, 4, 6, 8, 10]
print(linear_search(arr, 8))
medium
A. 3
B. 4
C. -1
D. Error
Solution
Step 1: Trace the linear search function
The function checks each element from index 0 to 4 for the target 8.
Step 2: Find the target index
At index 3, arr[3] is 8, so function returns 3.
Final Answer:
3 -> Option A
Quick Check:
Index of 8 in list is 3 [OK]
Hint: Linear search returns index of first match [OK]
Common Mistakes:
Confusing index with value
Assuming zero-based index starts at 1
Expecting output to be value instead of index
4. Find the error in this binary search implementation and select the correct fix:
C. Change mid calculation to mid = (low + high + 1) // 2
D. Change high = len(arr) to high = len(arr) - 1
Solution
Step 1: Check high initialization
Indexing in Python is zero-based, so last index is len(arr) - 1, not len(arr).
Step 2: Understand loop condition and indexing
Using high = len(arr) causes index out of range error when mid equals len(arr).
Final Answer:
Change high = len(arr) to high = len(arr) - 1 -> Option D
Quick Check:
High must be last valid index [OK]
Hint: High index = length minus one for zero-based lists [OK]
Common Mistakes:
Setting high to length instead of last index
Changing loop condition incorrectly
Modifying mid calculation unnecessarily
5. You have a sorted list of 10,000 elements and want to find a target value. Which search method is more efficient in terms of average number of comparisons, and why?
Options: A) Linear Search because it checks elements sequentially. B) Linear Search because it works on sorted lists. C) Binary Search because it reduces search space by half each step. D) Binary Search because it checks every element once.
hard
A. Linear Search because it checks elements sequentially.
B. Linear Search because it works on sorted lists.
C. Binary Search because it reduces search space by half each step.
D. Binary Search because it checks every element once.
Solution
Step 1: Analyze average comparisons for Linear Search
Linear Search checks elements one by one, so average comparisons ≈ 10,000/2 = 5,000.
Step 2: Analyze average comparisons for Binary Search
Binary Search halves the list each step, so comparisons ≈ log2(10,000) ≈ 14.
Step 3: Compare efficiency
Binary Search requires far fewer comparisons on large sorted lists.
Final Answer:
Binary Search because it reduces search space by half each step. -> Option C
Quick Check:
Binary Search is more efficient on sorted lists [OK]