0
0
DSA C++programming~20 mins

Binary Search vs Linear Search Real Cost Difference in DSA C++ - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Search Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Linear Search on Sorted Array
What is the output of the following C++ code that performs a linear search for the value 7 in a sorted array?
DSA C++
int linearSearch(int arr[], int n, int x) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == x) return i;
    }
    return -1;
}

int main() {
    int arr[] = {1, 3, 5, 7, 9, 11};
    int n = sizeof(arr) / sizeof(arr[0]);
    int index = linearSearch(arr, n, 7);
    if (index != -1) {
        std::cout << "Found at index " << index << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }
    return 0;
}
AFound at index 3
BFound at index 2
CNot found
DFound at index 4
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and linear search checks elements one by one.
Predict Output
intermediate
2:00remaining
Output of Binary Search on Sorted Array
What is the output of the following C++ code that performs a binary search for the value 7 in a sorted array?
DSA C++
int binarySearch(int arr[], int left, int right, int x) {
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == x) return mid;
        if (arr[mid] < x) 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, 0, n - 1, 7);
    if (index != -1) {
        std::cout << "Found at index " << index << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }
    return 0;
}
AFound at index 2
BFound at index 4
CNot found
DFound at index 3
Attempts:
2 left
💡 Hint
Binary search divides the search space in half each step.
🧠 Conceptual
advanced
2:00remaining
Time Complexity Comparison
Which statement correctly compares the time complexity of linear search and binary search on a sorted array of size n?
ABoth linear and binary search are O(n)
BLinear search is O(log n), binary search is O(n)
CLinear search is O(n), binary search is O(log n)
DBoth linear and binary search are O(log n)
Attempts:
2 left
💡 Hint
Think about how many elements each algorithm checks in the worst case.
🚀 Application
advanced
2:00remaining
Choosing Search Method for Unsorted Data
You have a large unsorted array and need to find if a value exists. Which search method is best and why?
ASort the array first then use linear search
BUse linear search because binary search requires sorted data
CUse binary search because it is always faster
DUse binary search without sorting because it works on any array
Attempts:
2 left
💡 Hint
Binary search needs sorted data to work correctly.
🔧 Debug
expert
3:00remaining
Why Does This Binary Search Fail?
What error or problem does the following binary search code have when searching for 7 in the array {1, 3, 5, 7, 9, 11}? int binarySearch(int arr[], int left, int right, int x) { while (left < right) { int mid = (left + right) / 2; if (arr[mid] == x) return mid; if (arr[mid] < x) 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, 0, n - 1, 7); if (index != -1) { std::cout << "Found at index " << index << std::endl; } else { std::cout << "Not found" << std::endl; } return 0; }
AIt may miss the target because the loop condition excludes right boundary
BIt causes an infinite loop due to incorrect mid calculation
CIt always returns -1 even if the element is present
DIt causes a runtime error due to out-of-bounds access
Attempts:
2 left
💡 Hint
Check the loop condition and how it affects the search range.