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 track the search range?
Two variables, usually called low and high, mark the start and end of the current search range within the sorted array.
Click to reveal answer
intermediate
Why is the middle index calculated as mid = low + (high - low) / 2 instead of (low + high) / 2?
Calculating mid as low + (high - low) / 2 prevents integer overflow when low and high are large numbers.
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 algorithm?
The time complexity is O(log n), meaning the search time grows slowly as the list size increases because the list is halved each step.
Click to reveal answer
What must be true about the array for binary search to work correctly?
AThe array must be unsorted
BThe array must contain only positive numbers
CThe array must be sorted
DThe array must have unique elements only
✗ Incorrect
Binary search requires the array to be sorted to decide which half to search next.
In iterative binary search, what happens if the middle element equals the target?
AReturn the middle index
BContinue searching the left half
CContinue searching the right half
DReturn -1
✗ Incorrect
If the middle element matches the target, the search is successful and the index is returned.
How is the middle index updated in each iteration?
Amid = low + (high - low) / 2
Bmid = (low + high) * 2
Cmid = low - high
Dmid = high - low
✗ Incorrect
The middle index is calculated safely as low plus half the distance between high and low.
What does it mean if low becomes greater than high during the search?
AThe search should restart
BThe target is not in the array
CThe array is empty
DThe target is at index low
✗ Incorrect
When low > high, the search range is empty, so the target is not found.
What is the worst-case time complexity of iterative binary search?
AO(1)
BO(n)
CO(n log n)
DO(log n)
✗ Incorrect
Binary search cuts the search space in half each time, leading to logarithmic time complexity.
Explain how the iterative binary search algorithm works step-by-step.
Think about how you look for a word in a dictionary by opening in the middle.
You got /5 concepts.
Describe why binary search is more efficient than linear search on sorted arrays.
Compare searching by dividing vs checking every item.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the while loop in the iterative binary search algorithm?
easy
A. To swap elements in the array
B. To sort the array before searching
C. To print all elements in the array
D. To repeatedly divide the search range until the target is found or range is empty
Solution
Step 1: Understand the role of the loop
The while loop runs as long as the search range is valid, narrowing down the range by half each time.
Step 2: Identify the loop's goal
The loop stops when the target is found or when the search range becomes empty, meaning the target is not present.
Final Answer:
To repeatedly divide the search range until the target is found or range is empty -> Option D
Quick Check:
Loop narrows search range until target found or range empty [OK]
Hint: Loop halves search range until target found or no range left [OK]
Common Mistakes:
Thinking the loop sorts the array
Assuming the loop swaps elements
Believing the loop prints elements
2. Which of the following is the correct way to calculate the middle index in an iterative binary search in C to avoid overflow?
easy
A. int mid = (low + high) / 2;
B. int mid = (low - high) / 2;
C. int mid = low + (high - low) / 2;
D. int mid = (low * high) / 2;
Solution
Step 1: Understand overflow risk
Adding low + high directly can overflow if both are large integers.
Since arr[mid] == target, the code prints 2 and breaks the loop.
Final Answer:
2 -> Option B
Quick Check:
Target found at index 2 [OK]
Hint: Check mid index where target matches first [OK]
Common Mistakes:
Confusing index with value
Not updating low/high correctly
Assuming target not found
4. Identify the error in the following iterative binary search code snippet:
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low < high) {
int mid = low + (high - low) / 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 while (low <= high)
B. The mid calculation causes overflow
C. The function returns mid instead of value
D. The array is not sorted before searching
Solution
Step 1: Analyze loop condition
The loop uses while (low < high), which misses checking when low equals high.
Step 2: Correct loop condition
It should be while (low <= high) to include the last element in search.
Final Answer:
The loop condition should be while (low <= high) -> Option A
Quick Check:
Loop must include low == high to check last element [OK]
Hint: Use <= in loop to check all elements including last [OK]
Common Mistakes:
Using < instead of <= in loop condition
Ignoring overflow in mid calculation (not here)
Confusing return value with index
Assuming array needs sorting inside function
5. Given a sorted array int arr[] = {1, 3, 5, 7, 9, 11, 13}, write the iterative binary search steps to find the target 10. What will be the final output of the search function?
hard
A. -1 (target not found)
B. Index 4
C. Index 5
D. Index 3
Solution
Step 1: Trace search for target 10
Initial low=0, high=6, mid=3 (arr[3]=7). Since 7 < 10, low=mid+1=4.
Step 2: Continue search narrowing range
Now low=4, high=6, mid=5 (arr[5]=11). Since 11 > 10, high=mid-1=4.
Step 3: Final check
Now low=4, high=4, mid=4 (arr[4]=9). Since 9 < 10, low=mid+1=5. Now low=5 > high=4, loop ends.
Final Answer:
-1 (target not found) -> Option A
Quick Check:
Target 10 not in array, returns -1 [OK]
Hint: If target not found, binary search returns -1 [OK]
Common Mistakes:
Assuming closest smaller element index is returned
Not updating low/high correctly
Stopping search too early
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.