What is the main advantage of using binary search over linear search?
Binary search is much faster because it cuts the search space in half each time, making it efficient for large sorted lists.
Click to reveal answer
beginner
Why must the list be sorted for binary search to work?
Because binary search compares the target with the middle element and decides which half to search next, the list must be sorted to know which half contains the target.
Click to reveal answer
beginner
What does 'sorted order' mean in the context of binary search?
Sorted order means the elements are arranged from smallest to largest (or vice versa), so you can predict where to find elements based on comparisons.
Click to reveal answer
intermediate
How does binary search reduce the number of comparisons compared to linear search?
Binary search divides the list in half each time, so it only needs about log₂(n) comparisons, while linear search may need up to n comparisons.
Click to reveal answer
beginner
What happens if you try to use binary search on an unsorted list?
Binary search will not work correctly because the assumption about sorted order is broken, leading to wrong decisions about which half to search.
Click to reveal answer
Why is a sorted list necessary for binary search?
ATo avoid duplicates
BTo make the list shorter
CTo decide which half of the list to search next
DTo use less memory
✗ Incorrect
Binary search relies on sorted order to know if the target is in the left or right half after comparing with the middle element.
What is the time complexity of binary search in terms of number of elements n?
AO(log n)
BO(1)
CO(n log n)
DO(n)
✗ Incorrect
Binary search cuts the search space in half each step, so it runs in logarithmic time O(log n).
If a list is not sorted, what will happen if you use binary search?
AIt will sort the list first
BIt will always find the target
CIt will find the target faster
DIt may give wrong results
✗ Incorrect
Binary search assumes sorted order; without it, the search decisions are invalid and results can be wrong.
Which of these is a benefit of sorted order besides binary search?
ASlower insertion
BEasier to find minimum and maximum
CMore memory usage
DNo duplicates allowed
✗ Incorrect
In a sorted list, the first element is the minimum and the last is the maximum, making them easy to find.
How many elements remain to search after the first step of binary search on a list of 16 elements?
A8
B15
C4
D1
✗ Incorrect
Binary search splits the list in half, so after one step, only 8 elements remain to search.
Explain why binary search requires a sorted list and how it uses this property to find an element.
Think about how knowing order helps decide where to look next.
You got /4 concepts.
Describe the difference in efficiency between linear search and binary search and why binary search is faster on large sorted lists.
Consider how many steps each method takes as the list grows.
You got /4 concepts.
Practice
(1/5)
1. Why is binary search only applicable on a sorted list?
easy
A. Because it can search any list regardless of order
B. Because it relies on comparing the middle element to decide which half to search next
C. Because it checks every element one by one
D. Because it sorts the list before searching
Solution
Step 1: Understand binary search logic
Binary search works by comparing the middle element to the target and deciding which half to continue searching.
Step 2: Importance of sorted order
This logic only works if the list is sorted, so we know which half contains the target based on comparison.
Final Answer:
Because it relies on comparing the middle element to decide which half to search next -> Option B
Quick Check:
Binary search needs sorted list = Because it relies on comparing the middle element to decide which half to search next [OK]
Hint: Binary search splits sorted list by middle comparison [OK]
Common Mistakes:
Thinking binary search works on unsorted lists
Confusing binary search with linear search
Assuming binary search sorts the list automatically
2. Which of the following is the correct syntax to find the middle index in a binary search on array arr between indices low and high in JavaScript?
easy
A. const mid = Math.floor((low + high) / 2);
B. const mid = (low + high) / 2;
C. const mid = Math.ceil((low + high) / 2);
D. const mid = low + high;
Solution
Step 1: Calculate middle index correctly
The middle index should be the floor of the average of low and high to get an integer index.
Step 2: Check each option
const mid = Math.floor((low + high) / 2); uses Math.floor which correctly rounds down. const mid = (low + high) / 2; returns a float, const mid = Math.ceil((low + high) / 2); rounds up which can cause errors, const mid = low + high; sums indices without division.
4. The following binary search code has a bug. What is the error?
function binarySearch(arr, target) {
let low = 0;
let high = arr.length;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
medium
A. The loop condition should be low < high, not low <= high
B. low should start at 1, not 0
C. mid calculation should use Math.ceil instead of Math.floor
D. high should be initialized to arr.length - 1, not arr.length
Solution
Step 1: Check initialization of high
Array indices go from 0 to length-1, so high must be arr.length - 1 to avoid out-of-bounds.
Step 2: Consequences of wrong high value
Setting high = arr.length causes mid to sometimes be arr.length, which is invalid index and causes errors.
Final Answer:
high should be initialized to arr.length - 1, not arr.length -> Option D
Quick Check:
High index = length - 1 [OK]
Hint: High index must be last valid array index [OK]
Common Mistakes:
Setting high to arr.length causing index errors
Changing low start incorrectly
Changing loop condition wrongly
5. You have a sorted array arr = [1, 3, 3, 5, 7, 7, 7, 9]. You want to find the first occurrence of the number 7 using binary search. Which modification helps achieve this?
hard
A. Use linear search instead of binary search
B. Return mid immediately when arr[mid] equals target
C. When arr[mid] equals target, continue searching left half to find first occurrence
D. Search right half when arr[mid] equals target
Solution
Step 1: Understand duplicate elements in sorted array
Binary search normally returns any matching index, but to find first occurrence, we must check if earlier indices also have the target.
Step 2: Modify search to continue left when target found
When arr[mid] equals target, update result but continue searching left half to find earlier occurrence.
Final Answer:
When arr[mid] equals target, continue searching left half to find first occurrence -> Option C
Quick Check:
Search left half on match for first occurrence [OK]
Hint: On match, search left half to find first occurrence [OK]
Common Mistakes:
Returning immediately on first match
Searching right half on match
Using linear search unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.