Challenge - 5 Problems
Master of In-Place Array Merging
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 code that merges two sorted arrays without using extra space?
DSA Javascript
function merge(arr1, arr2) {
let i = 0, j = 0, k = arr1.length - 1;
while (i <= k && j < arr2.length) {
if (arr1[i] < arr2[j]) {
i++;
} else {
[arr2[j], arr1[k]] = [arr1[k], arr2[j]];
j++;
k--;
}
}
arr1.sort((a,b) => a - b);
arr2.sort((a,b) => a - b);
return { arr1, arr2 };
}
const arr1 = [1, 5, 9, 10, 15, 20];
const arr2 = [2, 3, 8, 13];
console.log(merge(arr1, arr2));Attempts:
2 left
💡 Hint
Focus on how elements are swapped and then both arrays are sorted separately.
✗ Incorrect
The code swaps elements between arr1 and arr2 to ensure all smaller elements are in arr1. Then it sorts both arrays individually to maintain sorted order.
🧠 Conceptual
intermediateUnderstanding the space complexity of merging two sorted arrays without extra space
What is the space complexity of merging two sorted arrays without using extra space?
Attempts:
2 left
💡 Hint
No additional arrays or data structures are used in the merging process.
✗ Incorrect
The merging is done by swapping elements within the existing arrays and sorting them in place, so no extra space beyond a few variables is used.
🔧 Debug
advancedIdentify the error in this merge function
What error will this code produce when merging two sorted arrays without extra space?
DSA Javascript
function merge(arr1, arr2) {
let i = 0, j = 0, k = arr1.length - 1;
while (i < k && j < arr2.length) {
if (arr1[i] < arr2[j]) {
i++;
} else {
[arr2[j], arr1[k]] = [arr1[k], arr2[j]];
j++;
k--;
}
}
arr1.sort((a,b) => a - b);
arr2.sort((a,b) => a - b);
return { arr1, arr2 };
}
const arr1 = [1, 5, 9, 10, 15, 20];
const arr2 = [2, 3, 8, 13];
console.log(merge(arr1, arr2));Attempts:
2 left
💡 Hint
Check the loop condition carefully and think about the last element in arr1.
✗ Incorrect
The loop uses 'i < k' which skips comparing when i equals k, so the last element in arr1 might not be checked properly, causing incorrect merging.
❓ Predict Output
advancedOutput after merging arrays with duplicates
What is the output of merging these two sorted arrays with duplicates without extra space?
DSA Javascript
function merge(arr1, arr2) {
let i = 0, j = 0, k = arr1.length - 1;
while (i <= k && j < arr2.length) {
if (arr1[i] < arr2[j]) {
i++;
} else {
[arr2[j], arr1[k]] = [arr1[k], arr2[j]];
j++;
k--;
}
}
arr1.sort((a,b) => a - b);
arr2.sort((a,b) => a - b);
return { arr1, arr2 };
}
const arr1 = [1, 3, 5, 7, 9];
const arr2 = [3, 5, 7, 11];
console.log(merge(arr1, arr2));Attempts:
2 left
💡 Hint
Duplicates are swapped and sorted just like other elements.
✗ Incorrect
The smaller duplicates move to arr1 and larger duplicates remain in arr2, both sorted after swapping.
🧠 Conceptual
expertWhy is sorting both arrays after swapping necessary?
Why do we sort both arrays after swapping elements during the merge process without extra space?
Attempts:
2 left
💡 Hint
Think about what happens to the order inside each array after swapping.
✗ Incorrect
Swapping elements between arrays can place larger elements in the first array and smaller in the second, disrupting their sorted order, so sorting restores order.
