What is the main idea behind the binary search algorithm?
Binary search repeatedly divides a sorted list in half to find a target value quickly by comparing the middle element and deciding which half to search next.
Click to reveal answer
beginner
In the iterative binary search, what variables are commonly used to keep track of the search range?
Two variables, usually called low and high, mark the start and end indexes of the current search range in the array.
Click to reveal answer
intermediate
Why do we calculate the middle index as low + Math.floor((high - low) / 2) instead of (low + high) / 2?
Calculating middle this way prevents integer overflow when low and high are large numbers, ensuring safe and correct middle index calculation.
Click to reveal answer
beginner
What condition ends the iterative binary search loop?
The loop ends when low becomes greater than high, meaning the target is not found in the array.
Click to reveal answer
beginner
What is the time complexity of the binary search iterative approach and why?
The time complexity is O(log n) because the search range halves each step, quickly narrowing down the target position.
Click to reveal answer
What must be true about the array for binary search to work correctly?
AThe array must be sorted
BThe array must contain only positive numbers
CThe array must have unique elements
DThe array must be unsorted
✗ Incorrect
Binary search requires a sorted array to decide which half to search next.
In iterative binary search, if the middle element is less than the target, what happens next?
AReturn the middle index immediately
BSearch the left half by moving high
CSearch the right half by moving low
DStop the search
✗ Incorrect
If middle element is less than target, the target must be in the right half, so low is moved to middle + 1.
What does the iterative binary search return if the target is not found?
A-1
BThe index of the closest element
C0
DThe middle index
✗ Incorrect
Returning -1 is a common way to indicate the target is not found.
Which of these is the correct way to calculate the middle index safely?