0
0
DSA Cprogramming~20 mins

Binary Search as Divide and Conquer in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Binary Search Call
What is the output of the following C code snippet that performs a recursive binary search on a sorted array?

Assume the array is sorted in ascending order.
DSA C
int binarySearch(int arr[], int left, int right, int x) {
    if (right >= left) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == x) return mid;
        if (arr[mid] > x) return binarySearch(arr, left, mid - 1, x);
        return binarySearch(arr, mid + 1, right, x);
    }
    return -1;
}

int main() {
    int arr[] = {2, 4, 6, 8, 10, 12, 14};
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 10;
    int result = binarySearch(arr, 0, n - 1, x);
    printf("%d", result);
    return 0;
}
A4
B3
C-1
D5
Attempts:
2 left
💡 Hint
Remember array indexing starts at 0. Find the position of 10 in the array.
🧠 Conceptual
intermediate
1:30remaining
Divide and Conquer Principle in Binary Search
Which statement best describes how binary search uses the divide and conquer approach?
AIt divides the array into two halves and recursively searches only the half that may contain the target.
BIt divides the array into multiple parts and searches all parts simultaneously.
CIt searches the entire array linearly after dividing it into halves.
DIt combines all elements before searching for the target.
Attempts:
2 left
💡 Hint
Think about how binary search reduces the search space each step.
Predict Output
advanced
2:00remaining
Output of Iterative Binary Search with Missing Element
What is the output of the following iterative binary search code when searching for 7 in the given array?
DSA C
int binarySearchIter(int arr[], int n, int x) {
    int left = 0, right = n - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == x) return mid;
        else if (arr[mid] < x) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}

int main() {
    int arr[] = {1, 3, 5, 9, 11};
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 7;
    int result = binarySearchIter(arr, n, x);
    printf("%d", result);
    return 0;
}
A0
B2
C3
D-1
Attempts:
2 left
💡 Hint
7 is not in the array. What does the function return when the element is not found?
🔧 Debug
advanced
2:30remaining
Identify the Logical Error in Binary Search
Consider this binary search code snippet. What logical error causes it to fail for some inputs?
DSA C
int binarySearch(int arr[], int left, int right, int x) {
    if (left > right) return -1;
    int mid = (left + right) / 2;
    if (arr[mid] == x) return mid;
    if (arr[mid] < x) return binarySearch(arr, mid, right, x);
    else return binarySearch(arr, left, mid - 1, x);
}
AThe base case condition should be 'left >= right' instead of 'left > right'.
BThe recursive call uses 'mid' instead of 'mid + 1' when searching the right half, causing infinite recursion.
CThe mid calculation should be 'left - right' instead of 'left + right'.
DThe function should return 'left' instead of '-1' when element is not found.
Attempts:
2 left
💡 Hint
Check how the search space changes in recursive calls.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Binary Search in Worst Case
What is the worst-case time complexity of binary search on a sorted array of size n, and why?
AO(1), because it directly jumps to the middle element.
BO(n), because it may need to check every element.
CO(log n), because the search space halves with each step.
DO(n log n), because it divides and sorts the array first.
Attempts:
2 left
💡 Hint
Think about how many times you can divide n by 2 until you reach 1.