Challenge - 5 Problems
Merge Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Merge Sort on a small array
What is the output of the following JavaScript code that uses merge sort on the array [4, 1, 3, 2]?
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);
}
console.log(mergeSort([4, 1, 3, 2]));Attempts:
2 left
💡 Hint
Think about how merge sort splits and merges arrays in sorted order.
✗ Incorrect
Merge sort splits the array into halves recursively and merges them back in sorted order. The final output is the sorted array [1, 2, 3, 4].
🧠 Conceptual
intermediate1:00remaining
Time Complexity of Merge Sort
What is the time complexity of the merge sort algorithm in the average and worst cases?
Attempts:
2 left
💡 Hint
Consider 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
advanced2:00remaining
Identify the error in this merge function
What error will occur when running this merge function used in 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));
}Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop condition uses <= which allows i or j to equal the array length, causing undefined access and TypeError.
🚀 Application
advanced2:30remaining
Using Merge Sort to Sort Objects by a Key
Given an array of objects [{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Eve', age: 22}], which option correctly sorts the array by age 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].age < right[j].age) {
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 people = [{name: 'Bob', age: 25}, {name: 'Alice', age: 30}, {name: 'Eve', age: 22}];
console.log(mergeSort(people));Attempts:
2 left
💡 Hint
Check the comparison inside the merge function uses the age property.
✗ Incorrect
The merge function compares objects by their age property, so the sorted array is by ascending age: Eve (22), Bob (25), Alice (30).
🧠 Conceptual
expert1:30remaining
Space Complexity of Merge Sort
What is the space complexity of the merge sort algorithm and why?
Attempts:
2 left
💡 Hint
Think about the temporary arrays created during merging.
✗ Incorrect
Merge sort uses extra space proportional to the input size to store merged arrays, so space complexity is O(n).