0
0
DSA C++programming~20 mins

Binary Search Iterative 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 Binary Search on Sorted Array
What is the output of the following C++ code that performs an iterative binary search for the value 7 in the given sorted array?
DSA C++
#include <iostream>

int binarySearch(int arr[], int n, int target) {
    int left = 0, right = n - 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 main() {
    int arr[] = {1, 3, 5, 7, 9, 11};
    int n = sizeof(arr) / sizeof(arr[0]);
    int index = binarySearch(arr, n, 7);
    std::cout << index << std::endl;
    return 0;
}
A-1
B4
C2
D3
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and binary search returns the index of the target if found.
Predict Output
intermediate
2:00remaining
Result when target is not in array
What will be the output of the following C++ code when searching for the value 8 in the sorted array?
DSA C++
#include <iostream>

int binarySearch(int arr[], int n, int target) {
    int left = 0, right = n - 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 main() {
    int arr[] = {2, 4, 6, 10, 12};
    int n = sizeof(arr) / sizeof(arr[0]);
    int index = binarySearch(arr, n, 8);
    std::cout << index << std::endl;
    return 0;
}
A-1
B2
C3
D0
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
🧠 Conceptual
advanced
2:00remaining
Understanding the Mid Calculation in Binary Search
Why is the middle index calculated as mid = left + (right - left) / 2 instead of mid = (left + right) / 2 in the iterative binary search?
ATo ensure mid is always rounded up
BBecause it is faster to compute
CTo avoid integer overflow when left and right are large values
DTo handle negative indices correctly
Attempts:
2 left
💡 Hint
Think about what happens if left and right are very large integers.
🔧 Debug
advanced
2:00remaining
Identify the bug in this binary search code
What error will occur when running this binary search code?
DSA C++
int binarySearch(int arr[], int n, int target) {
    int left = 0, right = n;
    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;
}
AArray index out of bounds error
BInfinite loop
CNo error, works correctly
DCompilation error due to missing return
Attempts:
2 left
💡 Hint
Check the initial value of right and how it relates to array indices.
🚀 Application
expert
3:00remaining
Binary Search to Find First Occurrence of a Target
Given a sorted array with possible duplicates, which iterative binary search modification correctly returns the index of the first occurrence of the target value?
DSA C++
int firstOccurrence(int arr[], int n, int target) {
    int left = 0, right = n - 1;
    int result = -1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) {
            result = mid;
            right = mid - 1;
        } else if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return result;
}
AThe code above causes infinite loop for duplicates
BThe code above correctly finds the first occurrence index
CThe code above returns -1 even if target exists
DThe code above finds the last occurrence index instead
Attempts:
2 left
💡 Hint
When target is found, move right pointer to mid - 1 to search left side.