Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Binary Search for Existing Element
What is the output of the following C# code that performs an iterative binary search for the value 7 in a sorted array?
DSA C#
int BinarySearch(int[] arr, int target) { int left = 0, right = arr.Length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } int[] data = {1, 3, 5, 7, 9, 11}; int result = BinarySearch(data, 7); System.Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
✗ Incorrect
The value 7 is at index 3 in the array. The binary search finds it and returns the index.
❓ Predict Output
intermediateOutput of Binary Search for Non-Existing Element
What is the output of the following C# code that performs an iterative binary search for the value 8 in a sorted array?
DSA C#
int BinarySearch(int[] arr, int target) { int left = 0, right = arr.Length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } int[] data = {2, 4, 6, 10, 12}; int result = BinarySearch(data, 8); System.Console.WriteLine(result);
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
The value 8 is not in the array, so the function returns -1.
🧠 Conceptual
advancedWhy Use mid = left + (right - left) / 2 Instead of (left + right) / 2?
In the iterative binary search, why is the middle index calculated as
mid = left + (right - left) / 2 instead of mid = (left + right) / 2?Attempts:
2 left
💡 Hint
Think about what happens if left and right are very large numbers.
✗ Incorrect
Using (left + right) can cause integer overflow if left and right are large. The formula left + (right - left) / 2 avoids this by subtracting first.
🔧 Debug
advancedIdentify the Bug in Binary Search Implementation
What error will occur when running this iterative binary search code?
DSA C#
int BinarySearch(int[] arr, int target) { int left = 0, right = arr.Length; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
Attempts:
2 left
💡 Hint
Check the initial value of right and how it relates to array indices.
✗ Incorrect
The right boundary is set to arr.Length, which is out of valid index range (0 to Length-1). Accessing arr[mid] can cause IndexOutOfRangeException.
🚀 Application
expertNumber of Iterations in Worst Case Binary Search
Given a sorted array of 1024 elements, what is the maximum number of iterations the iterative binary search algorithm will perform to find a target or conclude it is not present?
Attempts:
2 left
💡 Hint
Binary search halves the search space each iteration. Think about powers of 2.
✗ Incorrect
The maximum iterations equal the smallest integer k where 2^k >= 1024. Since 2^10 = 1024, max iterations are 10.
