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 C++ code that merges two sorted arrays without using extra space?
DSA C++
void merge(int arr1[], int arr2[], int n, int m) { int i = n - 1, j = 0; while (i >= 0 && j < m) { if (arr1[i] > arr2[j]) { std::swap(arr1[i], arr2[j]); i--; j++; } else { break; } } std::sort(arr1, arr1 + n); std::sort(arr2, arr2 + m); } int main() { int arr1[] = {1, 5, 9, 10, 15, 20}; int arr2[] = {2, 3, 8, 13}; int n = 6, m = 4; merge(arr1, arr2, n, m); for (int i = 0; i < n; i++) std::cout << arr1[i] << " "; std::cout << "| "; for (int i = 0; i < m; i++) std::cout << arr2[i] << " "; return 0; }
Attempts:
2 left
💡 Hint
Focus on how the elements are swapped and then both arrays are sorted separately.
✗ Incorrect
The code swaps elements from the end of the first array with the start of the second array if the first array's element is greater. After swapping, both arrays are sorted individually, resulting in the merged sorted arrays without extra space.
❓ Predict Output
intermediateResulting arrays after merging with gap method
What will be the content of the two arrays after applying the gap method to merge them without extra space?
DSA C++
void merge(int arr1[], int arr2[], int n, int m) { int gap = (n + m + 1) / 2; while (gap > 0) { int i = 0, j = gap; while (j < n + m) { int a = (i < n) ? arr1[i] : arr2[i - n]; int b = (j < n) ? arr1[j] : arr2[j - n]; if (a > b) { if (i < n && j < n) std::swap(arr1[i], arr1[j]); else if (i < n && j >= n) std::swap(arr1[i], arr2[j - n]); else std::swap(arr2[i - n], arr2[j - n]); } i++; j++; } if (gap == 1) gap = 0; else gap = (gap + 1) / 2; } } int main() { int arr1[] = {10, 27, 38, 43}; int arr2[] = {3, 9, 82, 10, 15}; int n = 4, m = 5; merge(arr1, arr2, n, m); for (int i = 0; i < n; i++) std::cout << arr1[i] << " "; std::cout << "| "; for (int i = 0; i < m; i++) std::cout << arr2[i] << " "; return 0; }
Attempts:
2 left
💡 Hint
Remember the gap reduces each iteration and elements are swapped if out of order.
✗ Incorrect
The gap method compares elements at a distance 'gap' and swaps if needed, reducing the gap until zero. This results in both arrays merged in sorted order without extra space.
🔧 Debug
advancedIdentify the error in this merge function
What error will this code produce when merging two sorted arrays without extra space?
DSA C++
void merge(int arr1[], int arr2[], int n, int m) { int i = 0, j = 0; while (i < n && j < m) { if (arr1[i] > arr2[j]) { std::swap(arr1[i], arr2[j]); i++; j++; } else { i++; } } std::sort(arr1, arr1 + n); std::sort(arr2, arr2 + m); } int main() { int arr1[] = {1, 4, 7, 8, 10}; int arr2[] = {2, 3, 9}; int n = 5, m = 3; merge(arr1, arr2, n, m); for (int i = 0; i < n; i++) std::cout << arr1[i] << " "; std::cout << "| "; for (int i = 0; i < m; i++) std::cout << arr2[i] << " "; return 0; }
Attempts:
2 left
💡 Hint
Check how the indices i and j are incremented inside the loop.
✗ Incorrect
The code increments both i and j only when a swap happens, but if arr1[i] <= arr2[j], only i is incremented. This causes j to stay at 0, potentially missing elements in arr2, so the merge is incomplete.
🧠 Conceptual
advancedWhy is extra space not needed in some merge algorithms?
Why can two sorted arrays be merged without extra space using the gap method or swapping approach?
Attempts:
2 left
💡 Hint
Think about how sorting and swapping help avoid extra memory.
✗ Incorrect
The key is that the arrays are sorted, so by swapping out-of-place elements and then sorting each array, the merge can be done without extra space.
❓ Predict Output
expertFinal arrays after complex merge without extra space
Given the following arrays and merge code, what is the final printed output?
DSA C++
void merge(int arr1[], int arr2[], int n, int m) { int gap = n + m; for (gap = (gap + 1) / 2; gap > 0; gap = (gap == 1) ? 0 : (gap + 1) / 2) { int i = 0, j = gap; while (j < n + m) { int a = (i < n) ? arr1[i] : arr2[i - n]; int b = (j < n) ? arr1[j] : arr2[j - n]; if (a > b) { if (i < n && j < n) std::swap(arr1[i], arr1[j]); else if (i < n && j >= n) std::swap(arr1[i], arr2[j - n]); else std::swap(arr2[i - n], arr2[j - n]); } i++; j++; } } } int main() { int arr1[] = {1, 4, 7, 8, 10}; int arr2[] = {2, 3, 9, 15, 16, 20}; int n = 5, m = 6; merge(arr1, arr2, n, m); for (int i = 0; i < n; i++) std::cout << arr1[i] << " "; std::cout << "| "; for (int i = 0; i < m; i++) std::cout << arr2[i] << " "; return 0; }
Attempts:
2 left
💡 Hint
Trace the gap reduction and swapping carefully for all elements.
✗ Incorrect
The gap method reduces gap size each iteration, swapping out-of-order elements across both arrays. The final arrays are fully sorted and merged without extra space.
