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: low (start index) and high (end index) define the current search range within the sorted array.
Click to reveal answer
intermediate
Why do we calculate the middle index as mid = low + Math.floor((high - low) / 2) instead of (low + high) / 2?
To avoid potential overflow when low and high are large numbers, this formula safely calculates the middle index without exceeding number limits.
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 space halves with each step, quickly narrowing down the target position.
Click to reveal answer
What must be true about the array before applying binary search?
AThe array must be unsorted
BThe array must be sorted
CThe array must contain only positive numbers
DThe array must have unique elements only
✗ Incorrect
Binary search only works correctly on sorted arrays because it relies on order to decide which half to search.
In iterative binary search, if the middle element is less than the target, what happens next?
ASearch the left half by moving high
BStop the search and return -1
CSearch the right half by moving low
DReturn the middle index immediately
✗ Incorrect
If middle element is less than target, the target must be in the right half, so low is moved to mid + 1.
What does the iterative binary search return if the target is not found?
A-1
BThe index of the closest element
C0
Dnull
✗ Incorrect
Returning -1 is a common way to indicate the target is not present in the array.
Which of these is the correct way to calculate the middle index to avoid overflow?
Amid = (low + high) / 2
Bmid = (low - high) / 2
Cmid = high - low
Dmid = low + Math.floor((high - low) / 2)
✗ Incorrect
Calculating mid as low + Math.floor((high - low) / 2) prevents overflow when low and high are large.
What is the best description of the binary search algorithm?
ADivide and conquer by halving the search space
BLinear search through all elements
CRandomly pick elements until found
DSort the array then search linearly
✗ Incorrect
Binary search uses divide and conquer by halving the search space each step.
Explain how the iterative binary search algorithm works step-by-step.
Think about how you cut the search space in half each time.
You got /5 concepts.
Describe why binary search is faster than linear search on sorted arrays.
Focus on how many elements you check in each method.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the binary search iterative approach in a sorted array?
easy
A. To reverse the array elements
B. To find the position of a target value efficiently by repeatedly dividing the search interval in half
C. To sort the array elements in ascending order
D. To find the maximum value in the array
Solution
Step 1: Understand binary search goal
Binary search aims to find a target value's position quickly in a sorted array by dividing the search space repeatedly.
Step 2: Identify the iterative approach
The iterative method uses a loop to narrow down the search range until the target is found or the range is empty.
Final Answer:
To find the position of a target value efficiently by repeatedly dividing the search interval in half -> Option B
Quick Check:
Binary search = efficient position search [OK]
Hint: Binary search halves the search space each step [OK]
Common Mistakes:
Confusing binary search with sorting algorithms
Thinking it works on unsorted arrays
Assuming it finds maximum or minimum values
2. Which of the following is the correct way to initialize the mid index in an iterative binary search in JavaScript?
easy
A. const mid = Math.ceil((start + end) / 2);
B. const mid = (start + end) / 2;
C. const mid = Math.floor((start + end) / 2);
D. const mid = start + end;
Solution
Step 1: Understand index calculation
Mid index must be an integer index, so we use Math.floor to round down the average of start and end.
Step 2: Check options for correct syntax
const mid = Math.floor((start + end) / 2); uses Math.floor((start + end) / 2), which correctly calculates the middle index as an integer.