Challenge - 5 Problems
Merge Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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, 5}; int n = 5; 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 sorts the array in ascending order by dividing and merging.
✗ Incorrect
Merge sort divides the array into halves recursively and merges them in sorted order. The final sorted array is 1 2 5 5 9.
🧠 Conceptual
intermediateUnderstanding the Divide Step in Merge Sort
In merge sort, what is the main purpose of the divide step?
Attempts:
2 left
💡 Hint
Think about how merge sort breaks down the problem before sorting.
✗ Incorrect
The divide step breaks the array into smaller parts until each part has only one element, which is trivially sorted.
🔧 Debug
advancedIdentify the bug in the merge function
What error will occur when running this merge function on an array if the mergeSort calls are correct?
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 loop conditions when copying elements to temporary arrays.
✗ Incorrect
The loop copying elements to L uses i <= n1 which goes out of bounds since valid indices are 0 to n1-1.
❓ Predict Output
advancedOutput after partial merge sort calls
Given the array {8, 3, 7, 4} and the following calls, what is the state of the array after these calls?
DSA C++
int arr[] = {8, 3, 7, 4}; mergeSort(arr, 0, 1); mergeSort(arr, 2, 3);
Attempts:
2 left
💡 Hint
Each mergeSort call sorts only the specified subarray.
✗ Incorrect
The first call sorts indices 0 and 1: {8,3} → {3,8}. The second call sorts indices 2 and 3: {7,4} → {4,7}. The array becomes {3,8,4,7}.
🧠 Conceptual
expertTime Complexity of Merge Sort
What is the time complexity of merge sort and why?
Attempts:
2 left
💡 Hint
Think about how many times the array is split and how much work merging takes.
✗ Incorrect
Merge sort divides the array into halves log n times and merging each level takes O(n), so total is O(n log n).
