📖 Scenario: You have a sorted list of numbers representing book IDs in a library. You want to find if a specific book ID exists using a fast method.
🎯 Goal: Build a recursive binary search function in TypeScript to find a book ID in the sorted list.
📋 What You'll Learn
Create a sorted array of book IDs
Set the target book ID to search for
Write a recursive binary search function
Print the index of the found book ID or -1 if not found
💡 Why This Matters
🌍 Real World
Binary search is used in many software systems to quickly find items in sorted lists, such as searching for a book in a library database or looking up a contact in a phone book.
💼 Career
Understanding binary search and recursion is fundamental for software developers, especially when working with algorithms that require efficient searching and data handling.
Progress0 / 4 steps
1
Create the sorted array of book IDs
Create a variable called bookIDs and assign it the sorted array [101, 203, 305, 407, 509, 611, 713].
DSA Typescript
Hint
Use const to create the array with the exact numbers in order.
2
Set the target book ID to search
Create a variable called targetID and set it to 407.
DSA Typescript
Hint
Use const to create the target ID variable with the exact value.
3
Write the recursive binary search function
Write a recursive function called binarySearch that takes parameters arr, target, left, and right. Use the middle index to compare and recursively search the left or right half. Return the index if found, otherwise return -1.
DSA Typescript
Hint
Use Math.floor to find the middle index. Compare arr[mid] with target. Call binarySearch recursively on the left or right half.
4
Print the search result
Call binarySearch with bookIDs, targetID, 0, and bookIDs.length - 1. Print the returned index using console.log.
DSA Typescript
Hint
Call binarySearch with the full array range and print the result.
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?