0
0
DSA C++programming~20 mins

Binary Search Recursive Approach 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 on Sorted Array
What is the output of the following C++ code that performs a recursive binary search for the value 7 in the array?
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[] = {1, 3, 5, 7, 9, 11};
    int n = sizeof(arr) / sizeof(arr[0]);
    int result = binarySearch(arr, 0, n - 1, 7);
    std::cout << result << 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 found element.
Predict Output
intermediate
2:00remaining
Result of Recursive Binary Search When Element Not Present
What will be the output of this recursive binary search code when searching for 8 in the array?
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, 10, 12};
    int n = sizeof(arr) / sizeof(arr[0]);
    int result = binarySearch(arr, 0, n - 1, 8);
    std::cout << result << std::endl;
    return 0;
}
A0
B2
C3
D-1
Attempts:
2 left
💡 Hint
If the element is not found, the function returns -1.
🧠 Conceptual
advanced
1:30remaining
Understanding Recursive Binary Search Base Case
In the recursive binary search function, what is the purpose of the base case condition 'if (right < left)'?
AIt means the search space is empty and the element is not present.
BIt indicates the element has been found at index right.
CIt resets the search to the full array again.
DIt swaps the left and right indices to continue searching.
Attempts:
2 left
💡 Hint
Think about what happens when the search boundaries cross.
Predict Output
advanced
2:00remaining
Output of Recursive Binary Search with Duplicate Elements
Given the array with duplicates, what index will the recursive binary search return when searching for 5?
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[] = {1, 3, 5, 5, 5, 7, 9};
    int n = sizeof(arr) / sizeof(arr[0]);
    int result = binarySearch(arr, 0, n - 1, 5);
    std::cout << result << std::endl;
    return 0;
}
A4
B3
C2
D-1
Attempts:
2 left
💡 Hint
Binary search returns the first found index, not necessarily the first occurrence.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Recursive Binary Search
What is the time complexity of the recursive binary search algorithm on a sorted array of size n?
AO(n log n)
BO(n)
CO(log n)
DO(1)
Attempts:
2 left
💡 Hint
Each recursive call halves the search space.