What is the main idea behind the binary search algorithm?
Binary search works by repeatedly dividing a sorted list in half to find a target value quickly. It compares the target with the middle element and decides which half to search next.
Click to reveal answer
beginner
In the recursive binary search, what are the base cases?
The base cases are when the search range is empty (start index > end index), meaning the target is not found, or when the middle element equals the target, meaning the search is successful.
Click to reveal answer
beginner
Why must the input array be sorted for binary search to work?
Because binary search decides which half to search based on comparisons, the array must be sorted to ensure that all smaller elements are on one side and larger on the other.
Click to reveal answer
intermediate
What is the time complexity of binary search and why?
The time complexity is O(log n) because each step cuts the search space in half, reducing the problem size exponentially until the target is found or the search space is empty.
Click to reveal answer
intermediate
How does recursion help in implementing binary search?
Recursion allows the function to call itself with a smaller search range, simplifying the code by handling the search in smaller parts until the base case is reached.
Click to reveal answer
What should happen if the middle element equals the target in recursive binary search?
AReturn -1
BReturn the middle index
CSearch the right half
DSearch the left half
✗ Incorrect
If the middle element matches the target, the search is successful and the function returns the middle index.
What is the first step in each recursive call of binary search?
ACalculate the middle index of the current search range
BSort the array
CReturn -1
DSwap elements
✗ Incorrect
Each recursive call calculates the middle index to compare the target with the middle element.
What happens if the start index becomes greater than the end index in recursive binary search?
AReset the indices
BContinue searching
CThe target is not found, return -1
DReturn the start index
✗ Incorrect
When start > end, the search range is empty, so the target is not in the array.
Which of these is NOT a requirement for binary search to work correctly?
AArray must be sorted
BSearch range indices must be valid
CTarget must be comparable
DArray must have unique elements
✗ Incorrect
Binary search works with duplicates as well; uniqueness is not required.
What is the advantage of using recursion in binary search?
ASimplifies code by handling smaller subproblems
BMakes the search faster than iterative
CUses more memory without benefit
DAvoids calculating middle index
✗ Incorrect
Recursion breaks the problem into smaller parts, making the code easier to understand and write.
Explain how recursive binary search works step-by-step on a sorted array.
Think about dividing the array and calling the function again with smaller parts.
You got /6 concepts.
Describe the base cases in recursive binary search and why they are important.
Base cases tell the function when to stop searching.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the binary search recursive approach?
easy
A. Search each element one by one from start to end
B. Divide the array into halves and search recursively in the correct half
C. Sort the array after each recursive call
D. Use a queue to store elements for searching
Solution
Step 1: Understand binary search logic
Binary search works by dividing the sorted array into two halves and deciding which half to search next based on the middle element.
Step 2: Apply recursion in binary search
The recursive approach calls the same function on the half where the target might be, reducing the search space each time.
Final Answer:
Divide the array into halves and search recursively in the correct half -> Option B
Quick Check:
Binary search = divide and conquer [OK]
Hint: Remember: binary search splits array, then recurses [OK]
Common Mistakes:
Thinking binary search checks every element
Sorting array inside recursion
Using linear search logic
2. Which of the following is the correct TypeScript function signature for a recursive binary search?
easy
A. function binarySearch(arr: number[], target: string): boolean
B. function binarySearch(arr: string[], target: number): number
C. function binarySearch(arr: number[], target: number, left: number, right: number): number
D. function binarySearch(arr: number[], target: number): void
Solution
Step 1: Check parameters needed for recursion
Recursive binary search needs the array, target, and the current left and right indices to search between.
Step 2: Verify return type
The function returns the index of the target if found, so the return type is number.
Final Answer:
function binarySearch(arr: number[], target: number, left: number, right: number): number -> Option C
Quick Check:
Parameters + return type match recursive binary search [OK]
Hint: Recursive binary search needs left and right indices [OK]
Common Mistakes:
Missing left and right parameters
Wrong return type (void or boolean)
Mismatched array and target types
3. What will be the output of the following TypeScript code?
A. The base case condition should be left > right, not left >= right
B. The mid calculation is incorrect
C. The function should return boolean instead of number
D. The recursive calls have wrong parameters
Solution
Step 1: Analyze base case condition
The base case currently returns -1 if left >= right, but it should allow left == right to check that element.
Step 2: Correct base case to left > right
Changing to left > right ensures the search includes the last element before stopping.
Final Answer:
The base case condition should be left > right, not left >= right -> Option A
Quick Check:
Base case must allow left == right [OK]
Hint: Base case stops when left passes right, not equals [OK]
Common Mistakes:
Stopping search too early with wrong base case
Miscomputing mid index
Wrong return type
5. Given a sorted array arr = [1, 3, 5, 7, 9, 11, 13], write a recursive binary search call to find the index of 7. What will be the sequence of mid indices checked?