What is the main idea behind the Merge Sort algorithm?
Merge Sort divides the list into smaller parts, sorts each part, and then merges 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 is O(n log n) for both average and worst cases, where n is the number of elements.
Click to reveal answer
intermediate
Why does Merge Sort use extra space?
Merge Sort uses extra space because it creates temporary arrays to hold sorted parts before merging them.
Click to reveal answer
beginner
Show the result of merging two sorted arrays: [1, 4, 7] and [2, 3, 6].
The merged sorted array is [1, 2, 3, 4, 6, 7].
Click to reveal answer
beginner
What are the two main steps in the Merge Sort algorithm?
1. Divide the array into halves until each part has one element. 2. Merge the parts back together in sorted order.
Click to reveal answer
What technique does Merge Sort use to sort data?
ADynamic programming
BGreedy approach
CDivide and conquer
DBrute force
✗ Incorrect
Merge Sort splits the array into smaller parts and conquers by merging them sorted.
What is the space complexity of Merge Sort?
AO(n)
BO(1)
CO(log n)
DO(n log n)
✗ Incorrect
Merge Sort requires extra space proportional to the size of the array for merging.
Which of these is NOT true about Merge Sort?
AIt sorts in place without extra space.
BIt works well on linked lists.
CIt has O(n log n) time complexity.
DIt is stable.
✗ Incorrect
Merge Sort needs extra space and does not sort in place.
After dividing an array of 8 elements, how many single-element parts do you get?
A4
B8
C2
D16
✗ Incorrect
Dividing continues until each part has one element, so 8 elements become 8 parts.
What is the first step in the Merge Sort algorithm?
ASwap elements
BMerge two sorted arrays
CSort the entire array directly
DDivide the array into halves
✗ Incorrect
Merge Sort starts by dividing the array into smaller halves.
Explain how the Merge Sort algorithm sorts an array step-by-step.
Think about breaking down the problem and then combining solutions.
You got /4 concepts.
Describe the advantages and disadvantages of using Merge Sort.
Consider speed, memory, and use cases.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Merge Sort algorithm?
easy
A. Compare each element with every other element and swap if needed
B. Pick the smallest element and place it at the beginning repeatedly
C. Divide the list into halves, sort each half, then merge them
D. Use a pivot to partition the list and sort partitions recursively
Solution
Step 1: Understand the divide and conquer approach
Merge Sort splits the list into two halves recursively until single elements remain.
Step 2: Merge sorted halves
It then merges these halves back together in sorted order.
Final Answer:
Divide the list into halves, sort each half, then merge them -> Option C
Quick Check:
Merge Sort = Divide and Conquer [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 declare a function for merge sort in Go?
easy
A. func mergeSort(arr []int) []int {}
B. func mergeSort(arr int[]) int[] {}
C. func mergeSort(arr array of int) array of int {}
D. func mergeSort(arr []int) int {}
Solution
Step 1: Check Go function syntax for slices
In Go, slices are declared as []int for integer slices.
Step 2: Verify return type matches input type
Merge sort returns a sorted slice, so return type is also []int.
Final Answer:
func mergeSort(arr []int) []int {} -> Option A
Quick Check:
Go slice syntax = []int [OK]
Hint: Go slices use []type syntax, not array of type [OK]
Common Mistakes:
Using array syntax like int[] which is invalid in Go
Returning int instead of []int for sorted slice
Using incorrect parameter types like 'array of int'
3. What will be the output of the following Go code snippet using merge sort?
package main
import "fmt"
func mergeSort(arr []int) []int {
if len(arr) <= 1 {
return arr
}
mid := len(arr) / 2
left := mergeSort(arr[:mid])
right := mergeSort(arr[mid:])
return merge(left, right)
}
func merge(left, right []int) []int {
result := []int{}
i, j := 0, 0
for i < len(left) && j < len(right) {
if left[i] < right[j] {
result = append(result, left[i])
i++
} else {
result = append(result, right[j])
j++
}
}
result = append(result, left[i:]...)
result = append(result, right[j:]...)
return result
}
func main() {
arr := []int{5, 3, 8, 4, 2}
sorted := mergeSort(arr)
fmt.Println(sorted)
}
medium
A. [5 3 8 4 2]
B. [2 3 4 5 8]
C. [8 5 4 3 2]
D. [2 3 5 4 8]
Solution
Step 1: Trace the recursive splitting
The array splits into halves until single elements: [5,3,8,4,2] -> [5,3] and [8,4,2], then further splits.
Step 2: Merge sorted halves step-by-step
Merge [5] and [3] -> [3,5]; merge [8] and [4,2] -> [2,4,8]; finally merge [3,5] and [2,4,8] -> [2,3,4,5,8].
Final Answer:
[2 3 4 5 8] -> Option B
Quick Check:
Sorted array = [2 3 4 5 8] [OK]
Hint: Merge halves sorted individually to get full sorted array [OK]
Common Mistakes:
Assuming original array prints without sorting
Mixing up merge order causing wrong output
Forgetting to append remaining elements after loop
4. Identify the bug in this merge function snippet and select the fix:
func merge(left, right []int) []int {
result := []int{}
i, j := 0, 0
for i < len(left) && j < len(right) {
if left[i] <= right[j] {
result = append(result, left[i])
i++
} else {
result = append(result, right[j])
j++
}
}
result = append(result, left[i:]...)
result = append(result, right[i:]...)
return result
}
medium
A. Remove the last two append statements
B. Change comparison from <= to <
C. Initialize result with length of left + right
D. Change last append from right[i:] to right[j:]
Solution
Step 1: Analyze the append of remaining elements
After merging common parts, remaining elements from left[i:] and right[j:] must be appended.
Step 2: Spot the incorrect slice used
The code incorrectly appends right[i:] instead of right[j:], which can cause wrong elements or panic.
Final Answer:
Change last append from right[i:] to right[j:] -> Option D
Quick Check:
Append remaining right elements using j index [OK]
Hint: Use correct index variable when appending leftover elements [OK]
Common Mistakes:
Using wrong index variable for right slice leftover