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 Call
What is the output of the following Python code that uses recursive binary search to find the index of the number 7 in the list?
DSA Python
def binary_search(arr, low, high, x): if low > high: return -1 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) arr = [1, 3, 5, 7, 9, 11] result = binary_search(arr, 0, len(arr) - 1, 7) print(result)
Attempts:
2 left
💡 Hint
Think about the middle index and how the search narrows down.
✗ Incorrect
The list is [1,3,5,7,9,11]. The middle index is 2 (value 5). Since 7 > 5, search right half. Next mid is 3 (value 7), found at index 3.
🧠 Conceptual
intermediateDivide and Conquer in Binary Search
Which statement best describes how binary search uses the divide and conquer approach?
Attempts:
2 left
💡 Hint
Think about how the search space reduces each step.
✗ Incorrect
Binary search splits the list into two halves and only continues searching the half where the target could be, ignoring the other half.
❓ Predict Output
advancedOutput of Binary Search with Missing Element
What is the output of this code when searching for 8 in the sorted list?
DSA Python
def binary_search(arr, low, high, x): if low > high: return -1 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) 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, what does the function return?
✗ Incorrect
8 is not in the list. The function returns -1 when the search space is empty.
🔧 Debug
advancedIdentify the Error in Binary Search Implementation
What error will this binary search code produce when searching for 7 in the list?
DSA Python
def binary_search(arr, low, high, x): if low >= high: return -1 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) arr = [1, 3, 5, 7] result = binary_search(arr, 0, len(arr) - 1, 7) print(result)
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
✗ Incorrect
The base case uses 'low >= high' which excludes the case when low == high, missing the last element check. So it returns -1 even if element is present.
🚀 Application
expertNumber of Recursive Calls in Binary Search
Given a sorted list of 64 elements, what is the maximum number of recursive calls binary search will make to find an element or conclude it is not present?
Attempts:
2 left
💡 Hint
Think about how many times you can divide 64 by 2 until you reach 1.
✗ Incorrect
Binary search divides the search space in half each recursive call. For 64 elements (2^6), the worst case requires 7 recursive calls: log2(64) + 1 = 7.
