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 on Sorted Array
What is the output of the following C# code that performs binary search on a sorted array?
DSA C#
using System; class Program { static 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; } static void Main() { int[] sortedArr = {2, 4, 6, 8, 10, 12}; int index = BinarySearch(sortedArr, 8); Console.WriteLine(index); } }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and binary search returns the index of the target if found.
✗ Incorrect
The target 8 is at index 3 in the sorted array. Binary search finds it and returns 3.
🧠 Conceptual
intermediateWhy Must the Array Be Sorted for Binary Search?
Why is it necessary for the array to be sorted before performing binary search?
Attempts:
2 left
💡 Hint
Think about how binary search decides which half of the array to ignore.
✗ Incorrect
Binary search relies on the order of elements to eliminate half the search space each time. Without sorting, this logic fails.
❓ Predict Output
advancedResult of Binary Search on Unsorted Array
What will be the output of this C# code that runs binary search on an unsorted array?
DSA C#
using System; class Program { static 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; } static void Main() { int[] unsortedArr = {2, 4, 10, 6, 12, 8}; int index = BinarySearch(unsortedArr, 8); Console.WriteLine(index); } }
Attempts:
2 left
💡 Hint
Binary search assumes the array is sorted. What happens if it is not?
✗ Incorrect
Since the array is not sorted, binary search cannot reliably find the target and returns -1 indicating not found.
🔧 Debug
advancedFind the Bug in Binary Search Implementation
What is the bug in this binary search code that causes it to fail for some inputs?
DSA C#
int BinarySearch(int[] arr, int target) { int left = 0, right = arr.Length - 1; while (left < right) { int mid = (left + right) / 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 valid index range for the array.
✗ Incorrect
Array indices go from 0 to Length - 1. Initializing 'right' to Length causes out-of-range access.
🧠 Conceptual
expertWhy Binary Search is More Efficient Than Linear Search on Sorted Data
Why is binary search generally faster than linear search when searching in sorted data?
Attempts:
2 left
💡 Hint
Think about how many elements each algorithm checks in the worst case.
✗ Incorrect
Binary search cuts the search space in half each time, so it runs in O(log n) time, much faster than linear search's O(n).
