What is the main idea behind the divide and conquer approach in binary search?
Divide the sorted list into two halves, compare the middle element with the target, and then recursively search in the half where the target could be.
Click to reveal answer
beginner
Why must the list be sorted for binary search to work?
Because binary search decides which half to search next based on comparisons, the list must be sorted to know if the target is in the left or right half.
Click to reveal answer
beginner
In binary search, what happens if the middle element equals the target?
The search stops and returns the position of the middle element because the target is found.
Click to reveal answer
intermediate
How does binary search reduce the problem size at each step?
It halves the search space by choosing either the left or right half based on the comparison with the middle element.
Click to reveal answer
intermediate
What is the time complexity of binary search and why?
O(log n) because the search space is halved at each step, leading to a logarithmic number of steps relative to the list size.
Click to reveal answer
What condition must be true for binary search to work correctly?
AThe list must be reversed
BThe list must be unsorted
CThe list must contain only unique elements
DThe list must be sorted
✗ Incorrect
Binary search requires a sorted list to decide which half to search next.
If the middle element is greater than the target, where does binary search continue?
AIn the left half
BSearch ends
CAt the middle element again
DIn the right half
✗ Incorrect
If middle element is greater, the target can only be in the left half.
What is the base case for the recursive binary search?
AAll of the above
BWhen the list is empty
CWhen the target is not found
DWhen the middle element equals the target
✗ Incorrect
Base cases include finding the target, empty list, or target not found.
How many elements does binary search check at each step?
AAll elements
BHalf of the list
COne element (the middle)
DNone
✗ Incorrect
Binary search compares only the middle element at each step.
What is the worst-case time complexity of binary search?
AO(n)
BO(log n)
CO(n log n)
DO(1)
✗ Incorrect
Binary search halves the search space each time, leading to O(log n) complexity.
Explain how binary search uses the divide and conquer strategy to find a target in a sorted list.
Think about how the list is split and how the search space shrinks.
You got /4 concepts.
Describe the base cases in a recursive binary search and why they are important.
Base cases stop the recursion and give answers.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind Binary Search as a divide and conquer algorithm?
easy
A. It uses random guesses to find the target quickly.
B. It searches every element one by one until it finds the target.
C. It divides the search range in half each time to find the target efficiently.
D. It sorts the array before searching for the target.
Solution
Step 1: Understand divide and conquer in binary search
Binary search splits the sorted list into two halves and decides which half to search next.
Step 2: How binary search reduces search space
By halving the search range each time, it quickly narrows down where the target could be.
Final Answer:
It divides the search range in half each time to find the target efficiently. -> Option C
Quick Check:
Divide and conquer = halve search space [OK]
Hint: Binary search halves the search space each step [OK]
Common Mistakes:
Thinking binary search checks every element
Assuming binary search works on unsorted lists
Confusing binary search with linear search
2. Which of the following is the correct Python syntax to calculate the middle index in binary search for a list named arr with indices low and high?
easy
A. mid = (low + high) // 2
B. mid = (low + high) / 2
C. mid = low + high
D. mid = low - high // 2
Solution
Step 1: Understand integer division for index calculation
We need an integer index, so floor division (//) is used to get the middle index.
Step 2: Check each option for correctness
mid = (low + high) // 2 uses floor division correctly. mid = (low + high) / 2 uses float division, which is invalid for indexing. Options C and D do not calculate the middle index properly.
Final Answer:
mid = (low + high) // 2 -> Option A
Quick Check:
Use floor division for middle index [OK]
Hint: Use // for integer middle index in Python [OK]
Common Mistakes:
Using / which gives float index
Adding indices without dividing
Incorrect operator precedence
3. What will be the output of the following Python code?
A. Change 'high = len(arr)' to 'high = len(arr) - 1' to avoid index error.
B. Change 'low <= high' to 'low < high' to fix infinite loop.
C. Change 'mid = (low + high) // 2' to 'mid = (low + high + 1) // 2' for correct mid.
D. Remove 'return -1' to allow function to run longer.
Solution
Step 1: Check initialization of high index
Using 'high = len(arr)' causes out-of-range error because last valid index is len(arr) - 1.
Step 2: Correct high initialization
Set 'high = len(arr) - 1' to ensure mid index stays within valid range.
Final Answer:
Change 'high = len(arr)' to 'high = len(arr) - 1' to avoid index error. -> Option A
Quick Check:
High index must be last valid index [OK]
Hint: Set high to last index, not length [OK]
Common Mistakes:
Setting high to length causes index error
Changing loop condition incorrectly
Miscalculating mid index
5. Given a sorted list arr = [1, 3, 5, 7, 9, 11, 13], you want to find the smallest index where the value is greater than or equal to 8 using binary search. Which modification to the standard binary search will achieve this?
hard
A. Use linear search instead because binary search can't find this.
B. Adjust binary search to move 'high' to mid - 1 when arr[mid] >= 8, else move 'low' to mid + 1.
C. Return mid immediately when arr[mid] > 8 without further checks.
D. Modify binary search to always move 'low' to mid + 1 regardless of comparison.
Solution
Step 1: Understand the problem - find smallest index with value >= 8
This is a variant called lower bound search, requiring careful adjustment of pointers.
Step 2: Modify binary search to narrow down to smallest qualifying index
When arr[mid] >= 8, move 'high' to mid - 1 to check if smaller index also qualifies; else move 'low' to mid + 1.
Final Answer:
Adjust binary search to move 'high' to mid - 1 when arr[mid] >= 8, else move 'low' to mid + 1. -> Option B
Quick Check:
Lower bound search moves high left on >= condition [OK]
Hint: Move high left when mid value meets condition [OK]
Common Mistakes:
Returning immediately without checking smaller indices
Using linear search unnecessarily
Moving low pointer incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.