Binary Search Recursive Approach in DSA Typescript - Time & Space Complexity
We want to understand how the time taken by recursive binary search changes as the input size grows.
Specifically, how many steps does it take to find a number in a sorted list?
Analyze the time complexity of the following code snippet.
function binarySearch(arr: number[], target: number, left: number, right: number): number {
if (left > right) return -1;
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] > target) return binarySearch(arr, target, left, mid - 1);
else return binarySearch(arr, target, mid + 1, right);
}
This code searches for a target number in a sorted array by repeatedly dividing the search range in half.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call that halves the search range each time.
- How many times: At most, the function calls itself until the search range is empty, roughly halving the size each time.
Each step cuts the search space in half, so the number of steps grows slowly as the list gets bigger.
| Input Size (n) | Approx. Operations (recursive calls) |
|---|---|
| 10 | About 4 |
| 100 | About 7 |
| 1000 | About 10 |
Pattern observation: Doubling the input size adds only one more step, showing very slow growth.
Time Complexity: O(log n)
This means the number of steps grows very slowly, making the search efficient even for large lists.
[X] Wrong: "Binary search checks every element one by one, so it is O(n)."
[OK] Correct: Binary search does not check all elements; it cuts the search area in half each time, so it only needs about log n steps.
Understanding binary search's time complexity shows you can analyze efficient algorithms, a key skill in coding interviews and real projects.
What if we changed the array to be unsorted? How would the time complexity of searching change?
Practice
binary search recursive approach?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 BQuick Check:
Binary search = divide and conquer [OK]
- Thinking binary search checks every element
- Sorting array inside recursion
- Using linear search logic
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 CQuick Check:
Parameters + return type match recursive binary search [OK]
- Missing left and right parameters
- Wrong return type (void or boolean)
- Mismatched array and target types
function binarySearch(arr: number[], target: number, left: number, right: number): number {
if (left > right) return -1;
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] > target) return binarySearch(arr, target, left, mid - 1);
else return binarySearch(arr, target, mid + 1, right);
}
const nums = [2, 4, 6, 8, 10];
console.log(binarySearch(nums, 6, 0, nums.length - 1));Solution
Step 1: Identify middle index and compare
Initial call: left=0, right=4, mid=2. arr[2] = 6 matches target 6.Step 2: Return index when found
Since arr[mid] equals target, function returns mid = 2 immediately.Final Answer:
2 -> Option AQuick Check:
Target found at index 2 [OK]
- Miscomputing mid index
- Returning wrong index
- Confusing array values with indices
function binarySearch(arr: number[], target: number, left: number, right: number): number {
if (left >= right) return -1;
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] > target) return binarySearch(arr, target, left, mid - 1);
else return binarySearch(arr, target, mid + 1, right);
}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 AQuick Check:
Base case must allow left == right [OK]
- Stopping search too early with wrong base case
- Miscomputing mid index
- Wrong return type
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?Solution
Step 1: Calculate initial mid index
left=0, right=6, mid = floor((0+6)/2) = 3. arr[3] = 7 matches target immediately.Step 2: No further recursive calls needed
Since target found at first mid, no other mid indices are checked.Final Answer:
[3] -> Option DQuick Check:
Target found at first mid index 3 [OK]
- Assuming multiple recursive calls always happen
- Miscomputing mid index
- Confusing indices with values
Ready to prove it?
Pass the test and this pattern moves unknown
