0
0
DSA C++programming~20 mins

Merge Sort Algorithm in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Merge Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Merge Sort on a small array
What is the output of the following C++ code after performing merge sort on the array?
DSA C++
void merge(int arr[], int l, int m, int r) {
    int n1 = m - l + 1;
    int n2 = r - m;
    int L[n1], R[n2];
    for (int i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (int j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
    int i = 0, j = 0, k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

void mergeSort(int arr[], int l, int r) {
    if (l < r) {
        int m = l + (r - l) / 2;
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
        merge(arr, l, m, r);
    }
}

int main() {
    int arr[] = {5, 2, 9, 1};
    int n = sizeof(arr) / sizeof(arr[0]);
    mergeSort(arr, 0, n - 1);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A1 2 5 9
B9 5 2 1
C2 1 5 9
D1 5 2 9
Attempts:
2 left
💡 Hint
Remember that merge sort divides the array and merges sorted halves.
🧠 Conceptual
intermediate
1:00remaining
Time Complexity of Merge Sort
What is the time complexity of merge sort in the average and worst cases?
AO(log n)
BO(n log n)
CO(n^2)
DO(n)
Attempts:
2 left
💡 Hint
Think about how many times the array is divided and merged.
🔧 Debug
advanced
1:30remaining
Identify the bug in this merge function
What error will this merge function cause when run?
DSA C++
void merge(int arr[], int l, int m, int r) {
    int n1 = m - l + 1;
    int n2 = r - m;
    int L[n1], R[n2];
    for (int i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (int j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
    int i = 0, j = 0, k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
AArray index out of bounds error
BNo error, runs correctly
CInfinite loop
DCompilation error due to missing return
Attempts:
2 left
💡 Hint
Check the for loop conditions carefully.
Predict Output
advanced
2:00remaining
Output after sorting a reversed array
What is the output of the following code after merge sorting the array?
DSA C++
int main() {
    int arr[] = {10, 9, 8, 7, 6};
    int n = sizeof(arr) / sizeof(arr[0]);
    mergeSort(arr, 0, n - 1);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A6 8 7 9 10
B10 9 8 7 6
C7 6 8 9 10
D6 7 8 9 10
Attempts:
2 left
💡 Hint
Merge sort sorts in ascending order regardless of initial order.
🧠 Conceptual
expert
1:30remaining
Space complexity of merge sort
What is the space complexity of merge sort and why?
AO(log n) because of recursive call stack only
BO(1) because it sorts in place without extra space
CO(n) because it requires temporary arrays for merging
DO(n log n) because it copies the array multiple times
Attempts:
2 left
💡 Hint
Think about the extra arrays used during merging.