0
0
DSA C++programming~20 mins

Why Binary Search and What Sorted Order Gives You in DSA C++ - Challenge Your Understanding

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 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 <iostream>
int main() {
    int arr[] = {2, 4, 6, 8, 10, 12};
    int index = binarySearch(arr, 6, 8);
    std::cout << index << std::endl;
    return 0;
}
A3
B2
C-1
D4
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and binary search returns the index of the target if found.
🧠 Conceptual
intermediate
1:30remaining
Why Must the Array Be Sorted for Binary Search?
Why is it necessary for the array to be sorted before applying binary search?
ABecause binary search requires the array to be sorted in descending order only.
BBecause binary search only works on arrays with unique elements.
CBecause binary search compares the target with the middle element to decide which half to search next, which only works if the array is sorted.
DBecause binary search can only be applied to arrays of even length.
Attempts:
2 left
💡 Hint
Think about how binary search decides which half of the array to search next.
Predict Output
advanced
2:00remaining
Result 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++
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 <iostream>
int main() {
    int arr[] = {10, 2, 8, 4, 12, 6};
    int index = binarySearch(arr, 6, 4);
    std::cout << index << std::endl;
    return 0;
}
A-1
B0
C3
D2
Attempts:
2 left
💡 Hint
Binary search assumes sorted order. What happens if the array is not sorted?
🔧 Debug
advanced
2:30remaining
Identify 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 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;
}
AThe condition in the while loop should be 'left < right' instead of 'left <= right'.
BThe 'right' variable should be initialized to 'size - 1' instead of 'size'.
CThe 'left' variable should be initialized to 1 instead of 0.
DThe mid calculation should be 'mid = (left + right) / 2 + 1'.
Attempts:
2 left
💡 Hint
Check the valid index range for the array.
🧠 Conceptual
expert
1:30remaining
Why Binary Search is More Efficient Than Linear Search on Sorted Data
Why is binary search more efficient than linear search when the data is sorted?
ABecause binary search sorts the array first, then searches linearly.
BBecause binary search checks every element sequentially but faster due to sorting.
CBecause binary search uses hashing to find elements instantly.
DBecause binary search divides the search space in half each step, reducing the number of comparisons to logarithmic time.
Attempts:
2 left
💡 Hint
Think about how many elements are left to check after each step in binary search.