What is the main advantage of using binary search over linear search?
Binary search is much faster because it repeatedly divides the search space in half, reducing the number of comparisons from linear (n) to logarithmic (log n).
Click to reveal answer
beginner
Why must the data be sorted to use binary search?
Binary search relies on the data being sorted so it can decide which half of the data to ignore after each comparison, ensuring the search space shrinks correctly.
Click to reveal answer
beginner
What does 'sorted order' mean in the context of binary search?
Sorted order means the elements are arranged in increasing or decreasing order, so each element is in a predictable position relative to others.
Click to reveal answer
intermediate
How does sorted order help in reducing the search time?
Sorted order allows binary search to eliminate half of the remaining elements after each comparison, quickly narrowing down the possible location of the target.
Click to reveal answer
intermediate
What is the time complexity of binary search and why?
The time complexity is O(log n) because the search space is halved with each step, leading to a logarithmic number of steps relative to the number of elements.
Click to reveal answer
Why can binary search only be used on sorted data?
ABecause it needs to know which half to discard after each comparison
BBecause it compares every element one by one
CBecause it works only on numbers
DBecause it requires data to be in random order
✗ Incorrect
Binary search decides which half to ignore based on the order, so data must be sorted.
What happens to the search space after each step in binary search?
AIt doubles
BIt stays the same
CIt halves
DIt becomes empty
✗ Incorrect
Binary search halves the search space after each comparison.
What is the best case time complexity of binary search?
AO(log n)
BO(1)
CO(n)
DO(n log n)
✗ Incorrect
If the target is found at the middle element immediately, the time is O(1).
Which of these is NOT a requirement for binary search?
AData must be sorted
BData must be in an array or list
CData must allow random access
DData must be unique
✗ Incorrect
Data does not need to be unique for binary search to work.
If you have 1,000,000 sorted elements, about how many steps does binary search take in the worst case?
A20
B1,000,000
C100
D10,000
✗ Incorrect
Binary search takes about log2(1,000,000) ≈ 20 steps.
Explain why sorted order is essential for binary search and how it helps reduce search time.
Think about how knowing order helps decide which part to ignore.
You got /3 concepts.
Describe the step-by-step process of binary search on a sorted list.
Imagine looking for a word in a dictionary.
You got /4 concepts.
Practice
(1/5)
1. Why is it important for a list to be sorted before applying binary search?
easy
A. Because binary search can only search for numbers, not strings
B. Because binary search divides the list based on order to find elements quickly
C. Because binary search works only on lists with unique elements
D. Because binary search requires the list to be reversed
Solution
Step 1: Understand binary search working
Binary search works by repeatedly dividing the list into halves and deciding which half to search next based on order.
Step 2: Importance of sorted order
If the list is not sorted, dividing by order does not help because elements are not arranged predictably.
Final Answer:
Because binary search divides the list based on order to find elements quickly -> Option B
Quick Check:
Sorted list needed for binary search [OK]
Hint: Binary search needs sorted data to halve search space fast [OK]
Common Mistakes:
Thinking binary search works on unsorted lists
Confusing binary search with linear search
Believing binary search requires unique elements
2. Which of the following is the correct syntax to perform binary search on a sorted vector v in C++ using std::binary_search?
easy
A. std::binary_search(v.begin(), v.end(), target);
B. std::binary_search(v, target);
C. binary_search(v, 0, v.size(), target);
D. std::binary_search(target, v.begin(), v.end());
Solution
Step 1: Recall std::binary_search parameters
It takes two iterators marking the start and end of the range, and the target value.
Step 2: Match correct syntax
std::binary_search(v.begin(), v.end(), target); correctly uses v.begin(), v.end(), and target in that order.
Final Answer:
std::binary_search(v.begin(), v.end(), target); -> Option A
Quick Check:
Correct iterator usage in binary_search [OK]
Hint: Use begin() and end() iterators with binary_search [OK]
Common Mistakes:
Passing vector directly without iterators
Swapping target and iterator arguments
Using incorrect function signature
3. What will be the output of the following C++ code?
binary_search requires the vector to be sorted; otherwise, the result is undefined or incorrect.
Final Answer:
The vector is not sorted before binary_search -> Option C
Quick Check:
Unsorted vector breaks binary_search [OK]
Hint: Always sort before binary_search to avoid wrong results [OK]
Common Mistakes:
Ignoring the need to sort before searching
Assuming binary_search sorts internally
Thinking target absence causes error
5. Given a sorted vector v = {1, 3, 5, 7, 9, 11}, you want to find the smallest element greater than or equal to 6 using binary search logic. Which approach correctly achieves this?
hard
A. Use std::lower_bound to find the first element not less than 6
B. Use std::binary_search to check if 6 exists, then pick next element
C. Use std::upper_bound to find the first element greater than 6, then subtract one
D. Sort the vector again before searching
Solution
Step 1: Understand the problem
We want the smallest element >= 6, which is the definition of lower bound.
Step 2: Choose correct function
std::lower_bound returns iterator to first element not less than the target, perfect for this need.
Step 3: Analyze other options
binary_search only tells presence, upper_bound finds strictly greater elements, sorting again is unnecessary.
Final Answer:
Use std::lower_bound to find the first element not less than 6 -> Option A
Quick Check:
Lower bound finds smallest element >= target [OK]
Hint: Use lower_bound for smallest element >= target in sorted list [OK]
Common Mistakes:
Using binary_search to find element position
Confusing upper_bound with lower_bound
Unnecessary sorting before search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.