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
int binarySearch(int arr[], int size, int target) { int left = 0, right = size - 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; } #include <stdio.h> int main() { int arr[] = {2, 4, 6, 8, 10, 12}; int index = binarySearch(arr, 6, 8); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
Remember array indexing starts at 0 and the array is sorted.
✗ Incorrect
The target 8 is at index 3 in the sorted array. Binary search finds it correctly and returns 3.
🧠 Conceptual
intermediateWhy Must the Array Be Sorted for Binary Search?
Why is it necessary for the array to be sorted before applying binary search?
Attempts:
2 left
💡 Hint
Think about how binary search decides which half to ignore.
✗ Incorrect
Binary search relies on the array being sorted to decide whether to search the left or right half based on comparison with the middle element.
❓ Predict Output
advancedOutput of Binary Search on Unsorted Array
What is the output of the following C code that performs binary search on an unsorted array?
DSA C
int binarySearch(int arr[], int size, int target) { int left = 0, right = size - 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; } #include <stdio.h> int main() { int arr[] = {10, 8, 2, 4, 12, 6}; int index = binarySearch(arr, 6, 8); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
The array is not sorted. Think about how binary search behaves then.
✗ Incorrect
Binary search assumes sorted order. On an unsorted array, it may not find the target and returns -1.
🧠 Conceptual
advancedEffect of Sorted Order on Binary Search Efficiency
How does having a sorted array improve the efficiency of searching compared to an unsorted array?
Attempts:
2 left
💡 Hint
Think about how many elements you can ignore at each step in binary search.
✗ Incorrect
Binary search on sorted arrays cuts the search space in half each step, making it much faster than linear search on unsorted arrays.
🚀 Application
expertFinding Insert Position Using Binary Search
Given a sorted array, which option correctly returns the index where a target should be inserted to keep the array sorted if the target is not found?
DSA C
int findInsertPosition(int arr[], int size, int target) { int left = 0, right = size - 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 left; } #include <stdio.h> int main() { int arr[] = {1, 3, 5, 6}; int pos = findInsertPosition(arr, 4, 4); printf("%d\n", pos); return 0; }
Attempts:
2 left
💡 Hint
If target is not found, the function returns the left pointer as insert position.
✗ Incorrect
The target 4 should be inserted at index 2 to keep the array sorted. The function returns left which is 2.
