Challenge - 5 Problems
Merge Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember that merge sort divides the array and merges sorted halves.
✗ Incorrect
Merge sort sorts the array in ascending order by dividing and merging. The sorted array is 1 2 5 9.
🧠 Conceptual
intermediate1:00remaining
Time Complexity of Merge Sort
What is the time complexity of merge sort in the average and worst cases?
Attempts:
2 left
💡 Hint
Think about how many times the array is divided and merged.
✗ Incorrect
Merge sort divides the array into halves log n times and merges n elements each time, resulting in O(n log n).
🔧 Debug
advanced1: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++; } }
Attempts:
2 left
💡 Hint
Check the for loop conditions carefully.
✗ Incorrect
The loop for copying L uses i <= n1 which accesses one element beyond array size causing out of bounds error.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Merge sort sorts in ascending order regardless of initial order.
✗ Incorrect
The reversed array is sorted into ascending order: 6 7 8 9 10.
🧠 Conceptual
expert1:30remaining
Space complexity of merge sort
What is the space complexity of merge sort and why?
Attempts:
2 left
💡 Hint
Think about the extra arrays used during merging.
✗ Incorrect
Merge sort uses temporary arrays to merge halves, requiring O(n) extra space.