Challenge - 5 Problems
Master of In-Place Merge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of merging two sorted arrays without extra space
What is the output of the following Java code that merges two sorted arrays without using extra space?
DSA Java
public class MergeSortedArrays { public static void merge(int[] arr1, int[] arr2) { int n = arr1.length; int m = arr2.length; int i = 0, j = 0, k = n - 1; while (i <= k && j < m) { if (arr1[i] < arr2[j]) { i++; } else { int temp = arr2[j]; arr2[j] = arr1[k]; arr1[k] = temp; j++; k--; } } java.util.Arrays.sort(arr1); java.util.Arrays.sort(arr2); } public static void main(String[] args) { int[] arr1 = {1, 5, 9, 10, 15, 20}; int[] arr2 = {2, 3, 8, 13}; merge(arr1, arr2); System.out.print(java.util.Arrays.toString(arr1) + " "); System.out.print(java.util.Arrays.toString(arr2)); } }
Attempts:
2 left
💡 Hint
Focus on how elements are swapped between the end of the first array and the start of the second array, then both arrays are sorted.
✗ Incorrect
The code swaps elements from the end of the first array with smaller elements from the second array, then sorts both arrays. This results in the first array having the smallest elements and the second array having the larger elements, both sorted.
🧠 Conceptual
intermediateUnderstanding the key idea behind merging without extra space
What is the main idea behind merging two sorted arrays without using extra space?
Attempts:
2 left
💡 Hint
Think about how to avoid extra memory by rearranging elements inside the given arrays.
✗ Incorrect
The approach swaps elements between the two arrays so that the first array contains smaller elements and the second array contains larger elements, then sorts both arrays to complete the merge without extra space.
🔧 Debug
advancedIdentify the error in this merge implementation
What error will occur if we replace the line 'while (i <= k && j < m)' with 'while (i < k && j <= m)' in the merge method?
DSA Java
public static void merge(int[] arr1, int[] arr2) { int n = arr1.length; int m = arr2.length; int i = 0, j = 0, k = n - 1; while (i < k && j <= m) { if (arr1[i] < arr2[j]) { i++; } else { int temp = arr2[j]; arr2[j] = arr1[k]; arr1[k] = temp; j++; k--; } } java.util.Arrays.sort(arr1); java.util.Arrays.sort(arr2); }
Attempts:
2 left
💡 Hint
Check the loop condition and array index usage carefully.
✗ Incorrect
The condition 'j <= m' allows j to become equal to m, which is out of bounds for arr2 (indices go from 0 to m-1), causing ArrayIndexOutOfBoundsException.
❓ Predict Output
advancedResult after merging arrays with duplicates
What is the output of the following code after merging two sorted arrays with duplicate elements without extra space?
DSA Java
public class MergeSortedArrays { public static void merge(int[] arr1, int[] arr2) { int n = arr1.length; int m = arr2.length; int i = 0, j = 0, k = n - 1; while (i <= k && j < m) { if (arr1[i] < arr2[j]) { i++; } else { int temp = arr2[j]; arr2[j] = arr1[k]; arr1[k] = temp; j++; k--; } } java.util.Arrays.sort(arr1); java.util.Arrays.sort(arr2); } public static void main(String[] args) { int[] arr1 = {1, 3, 5, 7, 9}; int[] arr2 = {3, 5, 7, 11, 13}; merge(arr1, arr2); System.out.print(java.util.Arrays.toString(arr1) + " "); System.out.print(java.util.Arrays.toString(arr2)); } }
Attempts:
2 left
💡 Hint
Duplicates are swapped and sorted just like other elements.
✗ Incorrect
After swapping and sorting, duplicates are distributed between the two arrays maintaining sorted order without extra space.
🧠 Conceptual
expertWhy is sorting both arrays after swapping necessary?
In the merge without extra space approach, why do we sort both arrays after swapping elements?
Attempts:
2 left
💡 Hint
Think about what swapping achieves and what sorting completes.
✗ Incorrect
Swapping places smaller elements in the first array and larger in the second but does not guarantee order inside each array. Sorting each array after swapping ensures each is sorted individually.
