Challenge - 5 Problems
Binary Search Recursive Master
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 C# code that uses recursive binary search to find the index of number 7 in the array?
DSA C#
using System; class Program { static int BinarySearch(int[] arr, int left, int right, int target) { if (right < left) return -1; int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] > target) return BinarySearch(arr, left, mid - 1, target); else return BinarySearch(arr, mid + 1, right, target); } static void Main() { int[] numbers = {1, 3, 5, 7, 9, 11}; int index = BinarySearch(numbers, 0, numbers.Length - 1, 7); Console.WriteLine(index); } }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and 7 is in the middle of the array.
✗ Incorrect
The array is [1, 3, 5, 7, 9, 11]. The number 7 is at index 3 (0-based). The recursive binary search finds it and returns 3.
❓ Predict Output
intermediateResult when target is not in array
What will be printed when searching for number 8 in the array using the recursive binary search below?
DSA C#
using System; class Program { static int BinarySearch(int[] arr, int left, int right, int target) { if (right < left) return -1; int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] > target) return BinarySearch(arr, left, mid - 1, target); else return BinarySearch(arr, mid + 1, right, target); } static void Main() { int[] numbers = {2, 4, 6, 10, 12}; int index = BinarySearch(numbers, 0, numbers.Length - 1, 8); Console.WriteLine(index); } }
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
The number 8 is not in the array [2, 4, 6, 10, 12]. The binary search ends with no match and returns -1.
🔧 Debug
advancedIdentify the error in recursive binary search
What error will the following C# code produce when executed?
DSA C#
using System; class Program { static int BinarySearch(int[] arr, int left, int right, int target) { if (left > right) return -1; int mid = (left + right) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) return BinarySearch(arr, mid + 1, right, target); else return BinarySearch(arr, left, mid - 1, target); } static void Main() { int[] numbers = {1, 2, 3, 4, 5}; int index = BinarySearch(numbers, 0, numbers.Length - 1, 3); Console.WriteLine(index); } }
Attempts:
2 left
💡 Hint
Check the conditions for recursive calls and the order of comparisons.
✗ Incorrect
The original code had the recursive calls swapped causing infinite recursion. This corrected version searches the right half when arr[mid] < target and left half otherwise, so it prints 2.
🧠 Conceptual
advancedNumber of recursive calls in worst case
For an array of size 64, what is the maximum number of recursive calls made by a recursive binary search in the worst case?
Attempts:
2 left
💡 Hint
Binary search divides the array size by 2 each call. Think about powers of 2.
✗ Incorrect
Binary search halves the search space each call. For size 64, max calls = log2(64) + 1 = 6 + 1 = 7.
🚀 Application
expertFind the missing number using recursive binary search
Given a sorted array of unique integers from 1 to 10 with one number missing, which recursive binary search call will find the missing number 6 by checking the mid element and comparing it to expected values? Assume the function returns the missing number or -1 if none missing.
DSA C#
int FindMissing(int[] arr, int left, int right) { if (left > right) return -1; int mid = left + (right - left) / 2; if (arr[mid] != mid + 1) { if (mid == 0 || arr[mid - 1] == mid) return mid + 1; else return FindMissing(arr, left, mid - 1); } else { return FindMissing(arr, mid + 1, right); } } int[] arr = {1, 2, 3, 4, 5, 7, 8, 9, 10}; int missing = FindMissing(arr, 0, arr.Length - 1); Console.WriteLine(missing);
Attempts:
2 left
💡 Hint
The missing number is detected when the element does not match its expected value (index + 1).
✗ Incorrect
The function finds the first index where arr[mid] != mid + 1. For the given array, this happens at index 5, so missing number is 6.
