Challenge - 5 Problems
Binary Search Recursive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Recursive Binary Search on Sorted Array
What is the output of the following Go code that performs a recursive binary search for the value 7 in the array?
DSA Go
package main import "fmt" func binarySearch(arr []int, low, high, target int) int { if low > high { return -1 } mid := low + (high-low)/2 if arr[mid] == target { return mid } else if arr[mid] > target { return binarySearch(arr, low, mid-1, target) } else { return binarySearch(arr, mid+1, high, target) } } func main() { arr := []int{1, 3, 5, 7, 9, 11} result := binarySearch(arr, 0, len(arr)-1, 7) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the function returns the index of the target if found.
✗ Incorrect
The target 7 is at index 3 in the array. The recursive binary search finds it and returns 3.
❓ Predict Output
intermediate2:00remaining
Result of Recursive Binary Search When Target Not Present
What will be printed when searching for 8 in the sorted array using the recursive binary search function below?
DSA Go
package main import "fmt" func binarySearch(arr []int, low, high, target int) int { if low > high { return -1 } mid := low + (high-low)/2 if arr[mid] == target { return mid } else if arr[mid] > target { return binarySearch(arr, low, mid-1, target) } else { return binarySearch(arr, mid+1, high, target) } } func main() { arr := []int{2, 4, 6, 10, 12} result := binarySearch(arr, 0, len(arr)-1, 8) fmt.Println(result) }
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
Since 8 is not in the array, the function returns -1 indicating the target is absent.
🔧 Debug
advanced2:00remaining
Identify the Error in Recursive Binary Search Implementation
What error will occur when running this recursive binary search code?
DSA Go
package main import "fmt" func binarySearch(arr []int, low, high, target int) int { if low > high { return -1 } mid := low + (high-low)/2 if arr[mid] == target { return mid } else if arr[mid] > target { return binarySearch(arr, low, mid-1, target) } else { return binarySearch(arr, mid+1, high, target) } } func main() { arr := []int{1, 2, 3, 4, 5} result := binarySearch(arr, 0, len(arr)-1, 3) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
✗ Incorrect
The base case uses 'low >= high' which causes the function to return -1 even when low == high and the target is at that index. It should be 'low > high'.
🧠 Conceptual
advanced1:00remaining
Understanding Recursive Binary Search Time Complexity
What is the time complexity of the recursive binary search algorithm on a sorted array of size n?
Attempts:
2 left
💡 Hint
Each recursive call halves the search space.
✗ Incorrect
Binary search divides the array in half each time, so it runs in logarithmic time O(log n).
🚀 Application
expert3:00remaining
Find the Index of First Occurrence Using Recursive Binary Search
Given a sorted array with duplicate elements, which recursive binary search modification will correctly find the first occurrence index of the target value 5?
Consider the array: [1, 2, 5, 5, 5, 7, 9]
DSA Go
package main import "fmt" func firstOccurrence(arr []int, low, high, target int) int { if low > high { return -1 } mid := low + (high-low)/2 if arr[mid] == target { if mid == 0 || arr[mid-1] < target { return mid } else { return firstOccurrence(arr, low, mid-1, target) } } else if arr[mid] < target { return firstOccurrence(arr, mid+1, high, target) } else { return firstOccurrence(arr, low, mid-1, target) } } func main() { arr := []int{1, 2, 5, 5, 5, 7, 9} result := firstOccurrence(arr, 0, len(arr)-1, 5) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check if the mid element is the first occurrence or if you need to search left.
✗ Incorrect
The function finds the first index where 5 appears, which is index 2 in the array.