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 Java code after sorting the array using merge sort?
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]; 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++; } } public static void main(String[] args) { int[] arr = {5, 3, 8, 4, 2}; mergeSort(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Trace the merge steps carefully, the array is sorted in ascending order.
✗ Incorrect
The merge sort algorithm divides the array into halves recursively and merges them back in sorted order. The final output is the sorted array: 2 3 4 5 8.
🧠 Conceptual
intermediateUnderstanding Merge Sort Time Complexity
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 split 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) time complexity.
🔧 Debug
advancedIdentify the bug 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]; 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 for copying elements into L array.
✗ Incorrect
The for loop copying elements into L uses i <= n1 which goes out of bounds. It should be i < n1.
🚀 Application
advancedUsing Merge Sort to count inversions
How can merge sort be modified to count the number of inversions (pairs where a[i] > a[j] and i < j) in an array?
Attempts:
2 left
💡 Hint
Think about when elements from the right half are smaller than those in the left half during merging.
✗ Incorrect
During merge, when an element from the right half is placed before elements in the left half, it indicates inversions equal to the remaining elements in left half.
📝 Syntax
expertIdentify the syntax error in this merge sort snippet
Which option correctly fixes the syntax error in this code snippet?
DSA Java
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); } }
Attempts:
2 left
💡 Hint
Look carefully at the line declaring mid.
✗ Incorrect
Java requires a semicolon at the end of statements. Missing semicolon causes syntax error.
