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 printed output of the following C# code after performing merge sort on 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 iIndex = 0, jIndex = 0, k = l; while (iIndex < n1 && jIndex < n2) { if (L[iIndex] <= R[jIndex]) { arr[k] = L[iIndex]; iIndex++; } else { arr[k] = R[jIndex]; jIndex++; } k++; } while (iIndex < n1) { arr[k] = L[iIndex]; iIndex++; k++; } while (jIndex < n2) { arr[k] = R[jIndex]; jIndex++; k++; } } 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); for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i]); if (i != arr.Length - 1) Console.Write(" -> "); } Console.WriteLine(" -> null"); } }
Attempts:
2 left
💡 Hint
Trace how merge sort splits and merges the array step by step.
✗ Incorrect
Merge sort splits the array into halves recursively and merges them in sorted order. The final sorted array is 1, 2, 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 one element, which is trivially sorted.
🔧 Debug
advancedIdentify the error in this merge function
What error will occur when running this merge function on an array?
DSA C#
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 iIndex = 0, jIndex = 0, k = l; while (iIndex < n1 && jIndex < n2) { if (L[iIndex] <= R[jIndex]) { arr[k] = L[iIndex]; iIndex++; } else { arr[k] = R[jIndex]; jIndex++; } k++; } while (iIndex < n1) { arr[k] = L[iIndex]; iIndex++; k++; } while (jIndex < n2) { arr[k] = R[jIndex]; jIndex++; k++; } }
Attempts:
2 left
💡 Hint
Check the for loop conditions carefully for array indexing.
✗ Incorrect
The for loop 'for (int i = 0; i <= n1; i++)' accesses L[i] where i can be n1, which is out of bounds since L has size n1.
❓ Predict Output
advancedResult of merge sort on a reversed array
What is the output after merge sorting this reversed 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 iIndex = 0, jIndex = 0, k = l; while (iIndex < n1 && jIndex < n2) { if (L[iIndex] <= R[jIndex]) { arr[k] = L[iIndex]; iIndex++; } else { arr[k] = R[jIndex]; jIndex++; } k++; } while (iIndex < n1) { arr[k] = L[iIndex]; iIndex++; k++; } while (jIndex < n2) { arr[k] = R[jIndex]; jIndex++; k++; } } 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); for (int i = 0; i < arr.Length; i++) { Console.Write(arr[i]); if (i != arr.Length - 1) Console.Write(" -> "); } Console.WriteLine(" -> null"); } }
Attempts:
2 left
💡 Hint
Merge sort always sorts in ascending order by merging sorted halves.
✗ Incorrect
The reversed array is sorted ascending: 6, 7, 8, 9, 10 after merge sort.
🧠 Conceptual
expertTime complexity of merge sort explained
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 merges all elements each time, resulting in O(n log n) time.
