0
0
DSA Goprogramming~20 mins

Merge Sort Algorithm in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Merge Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Merge Sort on a small array
What is the output of the following Go code that implements merge sort on the array [4, 1, 3, 2]?
DSA Go
package main
import "fmt"

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 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 main() {
    arr := []int{4, 1, 3, 2}
    sorted := mergeSort(arr)
    fmt.Println(sorted)
}
A[1 3 2 4]
B[4 3 2 1]
C[1 2 3 4]
D[2 1 3 4]
Attempts:
2 left
💡 Hint
Think about how merge sort splits the array and merges sorted halves.
🧠 Conceptual
intermediate
1:00remaining
Time Complexity of Merge Sort
What is the time complexity of the merge sort algorithm in the average and worst cases?
AO(log n)
BO(n log n)
CO(n^2)
DO(n)
Attempts:
2 left
💡 Hint
Consider how many times the array is split and merged.
🔧 Debug
advanced
2:00remaining
Identify the bug in this merge function
What error will occur when running this merge function in Go?
DSA Go
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
}
Aruntime error: index out of range
Bcompilation error: missing return statement
Cinfinite loop
Dno error, runs correctly
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
🚀 Application
advanced
1:30remaining
Merge Sort on Linked List
Which of the following statements about applying merge sort to a singly linked list is TRUE?
AMerge sort cannot be applied to linked lists.
BMerge sort is inefficient on linked lists because random access is slow.
CMerge sort requires converting the linked list to an array first.
DMerge sort can be implemented efficiently on linked lists without extra space by changing pointers.
Attempts:
2 left
💡 Hint
Think about how merging works with linked lists compared to arrays.
Predict Output
expert
2:30remaining
Output of Merge Sort with Duplicate Elements
What is the output of the following Go code after sorting the array [5, 3, 8, 3, 9, 1, 5] using merge sort?
DSA Go
package main
import "fmt"

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 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 main() {
    arr := []int{5, 3, 8, 3, 9, 1, 5}
    sorted := mergeSort(arr)
    fmt.Println(sorted)
}
A[1 3 3 5 5 8 9]
B[1 3 5 3 5 8 9]
C[1 3 3 5 8 9]
D[9 8 5 5 3 3 1]
Attempts:
2 left
💡 Hint
Check how duplicates are handled and the stability of merge sort.