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 to find the index of the number 7?
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[] numbers = {1, 3, 5, 7, 9, 11}; int result = BinarySearch(numbers, 7); System.Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Remember array indexing starts at 0. Binary search returns the index where the target is found.
✗ Incorrect
The number 7 is at index 3 in the array. The binary search finds it and returns 3.
❓ Predict Output
intermediateBinary Search Result When Target Not Present
What will be printed when the binary search function is called with target 8 on the sorted array below?
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[] numbers = {2, 4, 6, 10, 12}; int result = BinarySearch(numbers, 8); System.Console.WriteLine(result);
Attempts:
2 left
💡 Hint
If the target is not found, binary search returns -1.
✗ Incorrect
The number 8 is not in the array, so the function returns -1.
🧠 Conceptual
advancedDivide and Conquer Principle in Binary Search
Which statement best describes how binary search uses the divide and conquer approach?
Attempts:
2 left
💡 Hint
Think about how binary search reduces the search space each step.
✗ Incorrect
Binary search splits the array into two halves and continues searching only the half where the target could be, ignoring the other half.
🔧 Debug
advancedIdentify the Bug in Binary Search Implementation
What error will occur when running this binary search code?
DSA C#
int BinarySearch(int[] arr, int target) { int left = 0, right = arr.Length; 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; } int[] numbers = {1, 2, 3, 4, 5}; int result = BinarySearch(numbers, 6); System.Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Check the value of 'right' and how it is used to access array elements.
✗ Incorrect
The variable 'right' is set to arr.Length, which is out of valid index range (max index is Length-1). Accessing arr[mid] when mid equals Length causes IndexOutOfRangeException.
🚀 Application
expertBinary Search to Find First Occurrence in Sorted Array with Duplicates
Given a sorted array with duplicates, which code snippet correctly finds the first index of the target using binary search?
Attempts:
2 left
💡 Hint
To find the first occurrence, update result and continue searching left half.
✗ Incorrect
Option B updates 'result' when target is found and moves 'right' to mid-1 to find earlier occurrences. This ensures the first index is found.
