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 JavaScript code after sorting the array using merge sort?
DSA Javascript
function merge(left, right) {
let result = [];
let i = 0, j = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result.push(left[i]);
i++;
} else {
result.push(right[j]);
j++;
}
}
return result.concat(left.slice(i)).concat(right.slice(j));
}
function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
const array = [5, 3, 8, 4];
console.log(mergeSort(array));Attempts:
2 left
💡 Hint
Think about how merge sort splits the array and merges sorted halves.
✗ Incorrect
Merge sort splits the array into halves recursively until single elements remain, then merges them back in sorted order. The final sorted array is [3, 4, 5, 8].
🧠 Conceptual
intermediateUnderstanding the divide step in merge sort
In merge sort, what is the main purpose of the divide step?
Attempts:
2 left
💡 Hint
Divide step breaks the problem into smaller pieces.
✗ Incorrect
The divide step splits the array into smaller parts until each part has only one element, which is the base case for merge sort.
❓ Predict Output
advancedOutput of merge function with two sorted arrays
What is the output of the merge function when merging these two sorted arrays: [1, 4, 7] and [2, 3, 6]?
DSA Javascript
function merge(left, right) {
let result = [];
let i = 0, j = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result.push(left[i]);
i++;
} else {
result.push(right[j]);
j++;
}
}
return result.concat(left.slice(i)).concat(right.slice(j));
}
console.log(merge([1, 4, 7], [2, 3, 6]));Attempts:
2 left
💡 Hint
The merge function combines two sorted arrays into one sorted array.
✗ Incorrect
The merge function compares elements from both arrays and adds the smaller one to the result, producing a fully sorted merged array.
🔧 Debug
advancedIdentify the error in this merge sort implementation
What error will occur when running this merge sort code?
DSA Javascript
function mergeSort(arr) {
if (arr.length < 1) return arr;
const mid = Math.floor(arr.length / 2);
const left = mergeSort(arr.slice(0, mid));
const right = mergeSort(arr.slice(mid));
return merge(left, right);
}
function merge(left, right) {
let result = [];
let i = 0, j = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result.push(left[i]);
i++;
} else {
result.push(right[j]);
j++;
}
}
return result.concat(left.slice(i)).concat(right.slice(j));
}
console.log(mergeSort([3, 1, 2]));Attempts:
2 left
💡 Hint
Check the base case condition for recursion.
✗ Incorrect
The base case uses 'arr.length < 1' which never stops recursion for arrays of length 1, causing infinite recursion and stack overflow.
🚀 Application
expertNumber of comparisons in merge sort for 8 elements
What is the maximum number of element comparisons performed by merge sort when sorting an array of 8 elements?
Attempts:
2 left
💡 Hint
Think about how many comparisons happen at each merge step and sum them.
✗ Incorrect
Merge sort performs at most n*log2(n) comparisons. For n=8, log2(8)=3, so max comparisons ≈ 8*3=24 but exact max is 17 due to merge steps.
