What is the main idea behind the Merge Sort algorithm?
Merge Sort works by dividing the list into smaller parts, sorting each part, and then merging them back together in order.
Click to reveal answer
intermediate
What is the time complexity of Merge Sort in the average and worst cases?
The time complexity of Merge Sort is O(n log n) for both average and worst cases, where n is the number of elements to sort.
Click to reveal answer
intermediate
Why is Merge Sort considered a stable sorting algorithm?
Merge Sort is stable because it keeps the original order of equal elements when merging the sorted parts.
Click to reveal answer
beginner
Show the basic steps of Merge Sort on the array [4, 1, 3, 2].
Step 1: Divide into [4,1] and [3,2] Step 2: Divide further to [4], [1], [3], [2] Step 3: Merge [4] and [1] to get [1,4] Step 4: Merge [3] and [2] to get [2,3] Step 5: Merge [1,4] and [2,3] to get [1,2,3,4]
Click to reveal answer
intermediate
What is the space complexity of Merge Sort and why?
Merge Sort has a space complexity of O(n) because it requires extra space to hold the temporary arrays during the merge process.
Click to reveal answer
What technique does Merge Sort use to sort an array?
ABrute force
BDivide and conquer
CDynamic programming
DGreedy approach
✗ Incorrect
Merge Sort divides the array into smaller parts, sorts them, and merges them back, which is the divide and conquer technique.
Which of the following is true about Merge Sort?
AIt is a stable sorting algorithm
BIt has a worst-case time complexity of O(n^2)
CIt sorts in place without extra space
DIt only works on sorted arrays
✗ Incorrect
Merge Sort is stable because it preserves the order of equal elements during merging.
What is the first step in the Merge Sort algorithm?
ADivide the array into halves
BMerge two sorted arrays
CCompare all elements
DSwap elements
✗ Incorrect
Merge Sort starts by dividing the array into two halves recursively.
How does Merge Sort combine the divided parts?
ABy reversing the arrays
BBy swapping elements randomly
CBy sorting each part separately without merging
DBy merging sorted subarrays
✗ Incorrect
Merge Sort merges the sorted subarrays to form a fully sorted array.
What is the space complexity of Merge Sort?
AO(1)
BO(log n)
CO(n)
DO(n^2)
✗ Incorrect
Merge Sort requires extra space proportional to the size of the array for merging, so space complexity is O(n).
Explain how Merge Sort sorts an array step-by-step.
Think about breaking the problem into smaller pieces and then combining them.
You got /4 concepts.
Describe the advantages and disadvantages of using Merge Sort.
Consider speed, memory use, and stability.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Merge Sort algorithm?
easy
A. Insert elements one by one into a sorted sublist
B. Sort the array by repeatedly swapping adjacent elements
C. Pick a pivot and partition the array around it
D. Divide the array into halves, sort each half, then merge them
Solution
Step 1: Understand the divide and conquer approach
Merge Sort splits the array into two halves recursively until single elements remain.
Step 2: Merge sorted halves
It then merges these sorted halves back together to form a sorted array.
Final Answer:
Divide the array into halves, sort each half, then merge them -> Option D
Quick Check:
Merge Sort = Divide, sort halves, merge [OK]
Hint: Remember: split, sort halves, then merge [OK]
Common Mistakes:
Confusing Merge Sort with Bubble Sort
Thinking Merge Sort uses a pivot like Quick Sort
Believing Merge Sort sorts in place without extra space
2. Which of the following is the correct way to split an array arr into two halves in JavaScript for Merge Sort?
easy
A. const mid = Math.floor(arr.length / 2); const left = arr.slice(0, mid); const right = arr.slice(mid);
B. const mid = arr.length / 2; const left = arr.splice(0, mid); const right = arr.splice(mid);
C. const mid = arr.length / 2; const left = arr.slice(mid); const right = arr.slice(0, mid);
D. const mid = Math.ceil(arr.length / 2); const left = arr.slice(mid); const right = arr.slice(0, mid);
Solution
Step 1: Calculate middle index correctly
Use Math.floor to get the middle index for even or odd lengths.
Step 2: Use slice to split without modifying original array
Slice(0, mid) gets left half; slice(mid) gets right half correctly.
Final Answer:
const mid = Math.floor(arr.length / 2); const left = arr.slice(0, mid); const right = arr.slice(mid); -> Option A
Quick Check:
Use Math.floor and slice for splitting [OK]
Hint: Use Math.floor and slice to split arrays safely [OK]
Common Mistakes:
Using splice which modifies the original array
Not flooring the middle index causing float errors
Swapping left and right halves incorrectly
3. What will be the output of the following JavaScript code using Merge Sort?
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 = [];
while (left.length && right.length) {
if (left[0] < right[0]) result.push(left.shift());
else result.push(right.shift());
}
return result.concat(left, right);
}
console.log(mergeSort([4, 1, 3, 2]));
medium
A. [1, 3, 2, 4]
B. [4, 3, 2, 1]
C. [1, 2, 3, 4]
D. [2, 1, 3, 4]
Solution
Step 1: Recursively split and sort the array
The array [4,1,3,2] is split into [4,1] and [3,2], then further until single elements.
Step 2: Merge sorted halves step-by-step
Merge [4] and [1] to [1,4], merge [3] and [2] to [2,3], then merge [1,4] and [2,3] to [1,2,3,4].
Final Answer:
[1, 2, 3, 4] -> Option C
Quick Check:
Sorted array = [1, 2, 3, 4] [OK]
Hint: Trace splits and merges carefully to find sorted output [OK]
Common Mistakes:
Confusing merge order causing wrong output
Not handling empty arrays in merge
Using shift inefficiently causing performance issues
4. Identify the bug in this Merge Sort implementation:
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 = [];
while (left.length && right.length) {
if (left[0] < right[0]) result.push(left.shift());
else result.push(right.shift());
}
return result.concat(left, right);
}
medium
A. Base case should be arr.length <= 1, not arr.length === 1
B. Using shift() inside merge causes infinite loop
C. Math.floor should be Math.ceil for mid calculation
D. merge function should return result.reverse()
Solution
Step 1: Check base case condition
Base case returns when array length is 1, but it should also handle empty arrays (length 0) to avoid errors.
Step 2: Correct base case to include empty arrays
Change condition to arr.length <= 1 to safely return arrays of length 0 or 1.
Final Answer:
Base case should be arr.length <= 1, not arr.length === 1 -> Option A
Quick Check:
Base case = arr.length <= 1 [OK]
Hint: Base case must handle empty arrays too [OK]
Common Mistakes:
Ignoring empty array base case causing stack overflow
Incorrect mid calculation causing wrong splits
Misunderstanding shift() usage as a bug
5. You want to sort a large array of objects by a numeric property score using Merge Sort. Which modification is needed in the merge function to correctly sort by score?
hard
A. Use JSON.stringify to compare objects
B. Compare left[0].score and right[0].score instead of elements directly
C. Convert objects to strings before comparing
D. Sort only keys, then map back to objects
Solution
Step 1: Understand sorting objects by property
Objects cannot be compared directly; comparison must use the numeric property score.
Step 2: Modify merge comparison to use score
Change condition to compare left[0].score and right[0].score to merge correctly.
Final Answer:
Compare left[0].score and right[0].score instead of elements directly -> Option B
Quick Check:
Compare object properties for sorting [OK]
Hint: Compare object properties, not whole objects [OK]
Common Mistakes:
Trying to compare objects directly causing NaN or errors
Using string conversion which breaks numeric order
Sorting keys separately losing object structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.