Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Binary Search on Sorted List
What is the output of this code when searching for the value 7 in the list?
DSA Python
def binary_search(arr, low, high, x): if high >= low: mid = (low + high) // 2 if arr[mid] == x: return mid elif arr[mid] > x: return binary_search(arr, low, mid - 1, x) else: return binary_search(arr, mid + 1, high, x) else: return -1 arr = [1, 3, 5, 7, 9, 11] result = binary_search(arr, 0, len(arr) - 1, 7) print(result)
Attempts:
2 left
💡 Hint
Remember that list indices start at 0 and binary search returns the index of the found element.
✗ Incorrect
The value 7 is at index 3 in the list. The recursive binary search finds it and returns 3.
❓ Predict Output
intermediateResult of Searching Missing Element
What will be printed when searching for 8 in the list using the recursive binary search?
DSA Python
def binary_search(arr, low, high, x): if high >= low: mid = (low + high) // 2 if arr[mid] == x: return mid elif arr[mid] > x: return binary_search(arr, low, mid - 1, x) else: return binary_search(arr, mid + 1, high, x) else: return -1 arr = [2, 4, 6, 10, 12] result = binary_search(arr, 0, len(arr) - 1, 8) print(result)
Attempts:
2 left
💡 Hint
If the element is not found, the function returns -1.
✗ Incorrect
The value 8 is not in the list, so the function returns -1 indicating not found.
🔧 Debug
advancedIdentify the Error in Recursive Binary Search
What error will this code produce when run?
DSA Python
def binary_search(arr, low, high, x): if high > low: mid = (low + high) // 2 if arr[mid] == x: return mid elif arr[mid] > x: return binary_search(arr, low, mid - 1, x) else: return binary_search(arr, mid + 1, high, x) else: return -1 arr = [1, 2, 3, 4, 5] print(binary_search(arr, 0, len(arr) - 1, 5))
Attempts:
2 left
💡 Hint
Check the base condition for recursion termination carefully.
✗ Incorrect
The base condition uses 'high > low' instead of 'high >= low', so when low equals high, the function does not check that element and returns -1 incorrectly.
🧠 Conceptual
advancedNumber of Recursive Calls in Binary Search
If you perform a recursive binary search on a sorted list of 64 elements, what is the maximum number of recursive calls made before the search ends?
Attempts:
2 left
💡 Hint
Binary search halves the search space each call. Think about powers of 2.
✗ Incorrect
Binary search divides the search space by 2 in each recursive call. For a list of 64 elements (2^6), the worst-case number of recursive calls is log₂(64) + 1 = 7.
❓ Predict Output
expertOutput of Recursive Binary Search with Duplicate Elements
What index will the recursive binary search return when searching for 5 in this list?
DSA Python
def binary_search(arr, low, high, x): if high >= low: mid = (low + high) // 2 if arr[mid] == x: return mid elif arr[mid] > x: return binary_search(arr, low, mid - 1, x) else: return binary_search(arr, mid + 1, high, x) else: return -1 arr = [1, 3, 5, 5, 5, 7, 9] print(binary_search(arr, 0, len(arr) - 1, 5))
Attempts:
2 left
💡 Hint
Binary search may return any index of the target if duplicates exist.
✗ Incorrect
The middle index is calculated as (0+6)//2=3. The element at index 3 is 5, so the function returns 3 immediately.
