Challenge - 5 Problems
Merge Sort Master
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 this list?
Initial list: 4 -> 2 -> 1 -> 3 -> null
Initial list: 4 -> 2 -> 1 -> 3 -> null
DSA C#
class Node { public int val; public Node next; public Node(int val) { this.val = val; this.next = null; } } Node MergeSort(Node head) { if (head == null || head.next == null) return head; Node mid = GetMiddle(head); Node rightHead = mid.next; mid.next = null; Node left = MergeSort(head); Node right = MergeSort(rightHead); return Merge(left, right); } Node GetMiddle(Node head) { Node slow = head, fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } Node Merge(Node l1, Node l2) { Node dummy = new Node(0); Node current = dummy; while (l1 != null && l2 != null) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } current = current.next; } current.next = (l1 != null) ? l1 : l2; return dummy.next; } // After calling MergeSort on the initial list, print the list nodes in order.
Attempts:
2 left
💡 Hint
Think about how merge sort divides 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?
Why is merge sort preferred over quicksort for sorting linked lists?
Attempts:
2 left
💡 Hint
Think about how linked lists store elements and how merge sort accesses them.
✗ Incorrect
Merge sort works well with linked lists because it only needs sequential access and can split and merge lists efficiently without random access. Quicksort requires random access which linked lists do not support efficiently.
🔧 Debug
advancedIdentify the Bug in Merge Function
What error will occur when running this merge function on two sorted linked lists?
DSA C#
Node Merge(Node l1, Node l2) {
Node dummy = new Node(0);
Node current = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
current.next = l1;
current.next = l2;
return dummy.next;
}Attempts:
2 left
💡 Hint
Look carefully at how current.next is assigned after the loop.
✗ Incorrect
The code assigns current.next twice after the loop, so the first assignment is overwritten. This causes the remaining nodes from either l1 or l2 to be lost, resulting in an incomplete merged list.
📝 Syntax
advancedCorrect Syntax for Recursive Merge Sort Call
Which option correctly calls merge sort recursively on the two halves of a linked list?
DSA C#
Node MergeSort(Node head) {
if (head == null || head.next == null) return head;
Node mid = GetMiddle(head);
Node rightHead = mid.next;
mid.next = null;
// Recursive calls here
???
}Attempts:
2 left
💡 Hint
Remember to split the list at mid and call merge sort on each half starting at head and rightHead.
✗ Incorrect
After splitting the list at mid, the left half starts at head and the right half starts at rightHead. Recursive calls must be made on these two heads.
🚀 Application
expertNumber of Recursive Calls in Merge Sort
Given a linked list of 8 nodes, how many total calls to MergeSort will be made during the full sort?
Attempts:
2 left
💡 Hint
Think about how merge sort splits the list and how many nodes are processed at each level.
✗ Incorrect
Merge sort splits the list into halves recursively until single nodes remain. For 8 nodes, the total calls equal 2*n - 1 = 15.
