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 Java code after performing merge sort on the array?
DSA Java
public class MergeSortTest { public static void mergeSort(int[] arr, int left, int right) { if (left < right) { int mid = (left + right) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); merge(arr, left, mid, right); } } public static void merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; i++) L[i] = arr[left + i]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k++] = L[i++]; } else { arr[k++] = R[j++]; } } while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; } public static void main(String[] args) { int[] arr = {5, 2, 9, 1, 5}; mergeSort(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Remember that merge sort divides the array and merges sorted halves.
✗ 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.
✗ Incorrect
The divide step breaks the array into smaller parts until each part has only one element, which is naturally sorted.
🔧 Debug
advancedIdentify the error in this merge function
What error will occur when running this merge function in merge sort?
DSA Java
public static void merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; i++) L[i] = arr[left + i]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k++] = L[i++]; } else { arr[k++] = R[j++]; } } while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; }
Attempts:
2 left
💡 Hint
Check the for loop that copies elements into L array.
✗ Incorrect
The for loop uses i <= n1 which goes out of bounds since arrays are 0-indexed and length is n1.
🚀 Application
advancedNumber of comparisons in merge sort
What is the approximate number of comparisons merge sort makes when sorting an array of size n?
Attempts:
2 left
💡 Hint
Think about how many times the array is split and merged.
✗ Incorrect
Merge sort divides the array log n times and does n comparisons per level, resulting in O(n log n) comparisons.
❓ Predict Output
expertOutput after partial merge sort steps
Given the array {8, 3, 7, 4} and the following partial merge sort steps, what is the array after merging the first two elements and the last two elements separately?
DSA Java
int[] arr = {8, 3, 7, 4}; // After splitting into two halves: {8, 3} and {7, 4} // After sorting each half separately: // First half sorted: {3, 8} // Second half sorted: {4, 7} // What is the array after these partial steps (before final merge)? // The array is updated in place.
Attempts:
2 left
💡 Hint
After sorting halves, each half is sorted but not merged together yet.
✗ Incorrect
Each half is sorted individually, so first half becomes 3,8 and second half 4,7. The array is [3,8,4,7] before final merge.
