Binary Search Algorithm
- 12Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the fundamental idea behind binary search?
What is the fundamental idea behind binary search?
12 questions
- 1
What is the fundamental idea behind binary search?
- A
Start from the beginning and compare each element until the target is found
- B
Use a hash table to map values to positions for instant lookup
- C
Divide the array into two halves and search both halves recursively
- D
Repeatedly halve the search range by comparing the middle element with the target
Correct answer
DRepeatedly halve the search range by comparing the middle element with the target
Explanation
Binary search works by repeatedly halving the search range. At each step, the middle element is compared with the target, and the search continues in the left or right half accordingly.
- A
- 2
Why should the formula M=(L+R)/2 be avoided in binary search implementations?
- A
It always returns a floating-point number, which cannot be used as an index
- B
It always rounds up, causing off-by-one errors
- C
It may cause integer overflow when L and R are both large, leading to incorrect results
- D
It is slower than using bitwise operations
Correct answer
CIt may cause integer overflow when L and R are both large, leading to incorrect results
Explanation
Adding two large integers can overflow the maximum value representable by the data type, causing the sum to wrap around and produce a negative or incorrect value. The safe alternative is M=L+(R−L)/2.
- A
- 3
What is the correct formula to compute the middle index in binary search to avoid overflow?
- A
M=L−(R−L)/2
- B
M=(L+R)/2
- C
M=L+(R−L)/2
- D
M=(R−L)/2
Correct answer
CM=L+(R−L)/2
Explanation
Using M=L+(R−L)/2 ensures that the difference (R−L) never exceeds the initial range length, avoiding overflow of L+R.
- A
- 4
In binary search for an exact target in a sorted array, what should you do when the middle element is less than the target?
- A
Set R=mid−1
- B
Set L=mid
- C
Terminate and return -1
- D
Set L=mid+1
Correct answer
DSet L=mid+1
Explanation
Since the array is sorted, if a[mid]<target, the target must be to the right, so the left boundary is updated to mid+1.
- A
- 5
What is the time complexity of binary search in the worst case?
- A
O(1)
- B
O(logN)
- C
O(N)
- D
O(NlogN)
Correct answer
BO(logN)
Explanation
Each step halves the search range, leading to at most log2N steps. Thus the worst-case time complexity is logarithmic.
- A
- 6
When implementing lower bound (first element ≥X), what is the purpose of the variable
ans?- A
To keep track of the smallest index that satisfies the condition
- B
To count the number of iterations
- C
To store the middle index for recursive calls
- D
To store the initial value of the target
Correct answer
ATo keep track of the smallest index that satisfies the condition
Explanation
The variable
ansis updated whenever an element satisfies the condition (a[mid]≥X), and by continuing to search to the left, we ensureansholds the first (leftmost) such index. - A
- 7
In the lower bound problem, if no element satisfies the condition, what value should be returned?
- A
0
- B
The last index of the array
- C
−1 or a sentinel like infinity
- D
The size of the array
Correct answer
C−1 or a sentinel like infinity
Explanation
If no element meets the condition, a sentinel value (e.g., −1 or ∞) is returned to indicate that no valid element exists.
- A
- 8
In the rotated sorted array problem (finding the minimum), what condition is used to split the array into prefix (true) and suffix (false)?
- A
a[mid]<a[0]
- B
a[mid]>a[last]
- C
a[mid]<a[mid−1]
- D
a[mid]>a[mid+1]
Correct answer
Ba[mid]>a[last]
Explanation
By comparing the middle element with the last element, elements greater than a[last] belong to the rotated larger part (true), and those ≤a[last] belong to the smaller part (false). The first false marks the minimum.
- A
- 9
What is the key insight for applying binary search to problems like finding the peak in a mountain array?
- A
Always compare the middle element with the first element
- B
Use a linear scan to find the peak first
- C
Use recursion without a base case
- D
Look for a condition that creates a prefix of true and suffix of false (or vice versa)
Correct answer
DLook for a condition that creates a prefix of true and suffix of false (or vice versa)
Explanation
The core idea is to define a condition that splits the array into two parts (e.g., increasing then decreasing). Binary search then finds the boundary, which corresponds to the answer.
- A
- 10
When binary searching for the square root of x with a given precision, when does the algorithm terminate?
- A
When the range [L,R] becomes empty
- B
When mid2 exactly equals x
- C
When L exceeds R
- D
When R−L is smaller than the required precision
Correct answer
DWhen R−L is smaller than the required precision
Explanation
Since exact equality is unlikely with real numbers, the loop continues until the interval length is less than the tolerance, at which point either L or R is an acceptable approximation.
- A
- 11
In binary search on real numbers, why do we not update boundaries with mid+1 or mid−1?
- A
Because the values are continuous and we need to avoid skipping the answer
- B
Because the array is not sorted
- C
Because we always want to include mid in the next range
- D
Because mid is a floating-point number and indices must be integers
Correct answer
ABecause the values are continuous and we need to avoid skipping the answer
Explanation
With real numbers, the answer is a point in a continuous interval. Using mid+1 would skip over potential values; instead, we set L=mid or R=mid to narrow the interval.
- A
- 12
What was the bug in Java's binary search implementation that went unnoticed for over nine years?
- A
It computed the middle index using (L+R)/2, causing overflow
- B
It used an incorrect comparison operator
- C
It returned the wrong index when the target was not found
- D
It did not handle empty arrays
Correct answer
AIt computed the middle index using (L+R)/2, causing overflow
Explanation
The Java library used M=(L+R)/2, which overflows for large L and R. This bug was present for more than nine years before being discovered.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.