Challenge - 5 Problems
Merge Sort Master
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 sorting the array using merge sort?
DSA C#
using System; class Program { static void Merge(int[] arr, int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int[] L = new int[n1]; int[] R = new int[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 i1 = 0, j1 = 0, k = l; while (i1 < n1 && j1 < n2) { if (L[i1] <= R[j1]) { arr[k++] = L[i1++]; } else { arr[k++] = R[j1++]; } } while (i1 < n1) arr[k++] = L[i1++]; while (j1 < n2) arr[k++] = R[j1++]; } static 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); } } static void Main() { int[] arr = {5, 2, 9, 1}; MergeSort(arr, 0, arr.Length - 1); foreach (int x in arr) Console.Write(x + " "); } }
Attempts:
2 left
💡 Hint
Trace how the array is split and merged step by step.
✗ Incorrect
Merge sort splits the array into halves recursively and merges them back in sorted order. The final sorted array is 1 2 5 9.
🧠 Conceptual
intermediateUnderstanding merge step in merge sort
During the merge step of merge sort, what is the main purpose of the two temporary arrays used?
Attempts:
2 left
💡 Hint
Think about how merge sort combines two sorted halves.
✗ Incorrect
The temporary arrays hold the left and right halves so the algorithm can compare and merge them in sorted order.
🔧 Debug
advancedIdentify the error in this merge sort implementation
What error will this code produce when run?
DSA C#
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); } }
Attempts:
2 left
💡 Hint
Check the base condition for recursion carefully.
✗ Incorrect
The base condition should be 'l < r' to stop recursion. Using 'l <= r' causes infinite recursion leading to stack overflow.
❓ Predict Output
advancedOutput after sorting a reversed array
What is the output of this C# code after applying merge sort to the array?
DSA C#
using System; class Program { static void Merge(int[] arr, int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int[] L = new int[n1]; int[] R = new int[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 i1 = 0, j1 = 0, k = l; while (i1 < n1 && j1 < n2) { if (L[i1] <= R[j1]) { arr[k++] = L[i1++]; } else { arr[k++] = R[j1++]; } } while (i1 < n1) arr[k++] = L[i1++]; while (j1 < n2) arr[k++] = R[j1++]; } static 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); } } static void Main() { int[] arr = {10, 9, 8, 7, 6}; MergeSort(arr, 0, arr.Length - 1); foreach (int x in arr) Console.Write(x + " "); } }
Attempts:
2 left
💡 Hint
Merge sort sorts the array in ascending order.
✗ Incorrect
The reversed array is sorted ascending: 6 7 8 9 10.
🧠 Conceptual
expertTime complexity of merge sort
What is the time complexity of merge sort in the worst case and why?
Attempts:
2 left
💡 Hint
Consider how many times the array is split and merged.
✗ Incorrect
Merge sort splits the array into halves (log n splits) and merges all elements (n) at each level, resulting in O(n log n) time.
