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 Linked List
What is the printed state of the linked list after applying merge sort on the list 4 -> 2 -> 1 -> 3 -> null?
DSA Go
type ListNode struct { Val int Next *ListNode } func printList(head *ListNode) string { if head == nil { return "null" } result := "" for head != nil { result += fmt.Sprintf("%d -> ", head.Val) head = head.Next } result += "null" return result } func mergeSort(head *ListNode) *ListNode { if head == nil || head.Next == nil { return head } slow, fast := head, head.Next for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next } mid := slow.Next slow.Next = nil left := mergeSort(head) right := mergeSort(mid) return merge(left, right) } func merge(l1, l2 *ListNode) *ListNode { dummy := &ListNode{} tail := dummy for l1 != nil && l2 != nil { if l1.Val < l2.Val { tail.Next = l1 l1 = l1.Next } else { tail.Next = l2 l2 = l2.Next } tail = tail.Next } if l1 != nil { tail.Next = l1 } else { tail.Next = l2 } return dummy.Next } func main() { head := &ListNode{4, &ListNode{2, &ListNode{1, &ListNode{3, nil}}}} sorted := mergeSort(head) fmt.Println(printList(sorted)) }
Attempts:
2 left
💡 Hint
Think about how merge sort splits and merges the list in sorted order.
✗ Incorrect
Merge sort splits the list into halves recursively and merges them back in sorted order. The final sorted list is 1 -> 2 -> 3 -> 4 -> null.
🧠 Conceptual
intermediateWhy Use Merge Sort for Linked Lists?
Which reason best explains why merge sort is preferred for sorting linked lists over quicksort or other algorithms?
Attempts:
2 left
💡 Hint
Think about how linked lists store data and how merge sort handles splitting and merging.
✗ Incorrect
Merge sort works well with linked lists because it only needs to change pointers and does not require random access like quicksort or heapsort.
🔧 Debug
advancedIdentify the Bug in Merge Function
What error will occur when running this merge function on two sorted linked lists?
DSA Go
func merge(l1, l2 *ListNode) *ListNode {
dummy := &ListNode{}
tail := dummy
for l1 != nil && l2 != nil {
if l1.Val <= l2.Val {
tail.Next = l1
l1 = l1.Next
} else {
tail.Next = l2
l2 = l2.Next
}
tail = tail.Next
}
tail.Next = l1
tail.Next = l2
return dummy.Next
}Attempts:
2 left
💡 Hint
Check how tail.Next is assigned after the loop.
✗ Incorrect
The code assigns tail.Next to l1, then immediately overwrites it with l2, losing the l1 tail. So only l2 elements remain after the loop.
❓ Predict Output
advancedOutput After Sorting a Reverse Sorted List
What is the printed linked list after sorting 5 -> 4 -> 3 -> 2 -> 1 -> null using merge sort?
DSA Go
head := &ListNode{5, &ListNode{4, &ListNode{3, &ListNode{2, &ListNode{1, nil}}}}}
sorted := mergeSort(head)
fmt.Println(printList(sorted))Attempts:
2 left
💡 Hint
Merge sort always sorts the list in ascending order.
✗ Incorrect
Merge sort splits and merges the list recursively, resulting in a sorted list from smallest to largest.
🧠 Conceptual
expertTime Complexity of Merge Sort on Linked List
What is the time complexity of merge sort when sorting a linked list of n nodes?
Attempts:
2 left
💡 Hint
Consider how many times the list is split and how merging works.
✗ Incorrect
Merge sort splits the list log n times and merges all n elements each time, resulting in O(n log n) complexity.
