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 this list?
Initial list: 4 -> 2 -> 1 -> 3 -> null
Initial list: 4 -> 2 -> 1 -> 3 -> null
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } public class Solution { public static ListNode mergeSort(ListNode head) { if (head == null || head.next == null) return head; ListNode mid = getMiddle(head); ListNode nextToMid = mid.next; mid.next = null; ListNode left = mergeSort(head); ListNode right = mergeSort(nextToMid); return merge(left, right); } private static ListNode getMiddle(ListNode head) { if (head == null) return head; ListNode slow = head, fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } private static ListNode merge(ListNode a, ListNode b) { ListNode dummy = new ListNode(0); ListNode tail = dummy; while (a != null && b != null) { if (a.val <= b.val) { tail.next = a; a = a.next; } else { tail.next = b; b = b.next; } tail = tail.next; } tail.next = (a != null) ? a : b; return dummy.next; } public static void printList(ListNode head) { while (head != null) { System.out.print(head.val); if (head.next != null) System.out.print(" -> "); head = head.next; } System.out.println(" -> null"); } public static void main(String[] args) { ListNode head = new ListNode(4); head.next = new ListNode(2); head.next.next = new ListNode(1); head.next.next.next = new ListNode(3); ListNode sorted = mergeSort(head); printList(sorted); } }
Attempts:
2 left
š” Hint
Merge sort splits the list and merges sorted halves.
ā Incorrect
Merge sort divides the list into halves recursively and merges them 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 a linked list?
Attempts:
2 left
š” Hint
Think about how linked lists store elements and how merge sort works.
ā Incorrect
Merge sort works well on linked lists because it only requires sequential access and merges sorted lists efficiently. Quicksort needs random access which linked lists do not provide efficiently.
š§ Debug
advancedIdentify the Bug in Merge Function
What error will occur when running this merge function on two sorted linked lists?
DSA Java
private static ListNode merge(ListNode a, ListNode b) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (a != null && b != null) {
if (a.val < b.val) {
tail.next = a;
a = a.next;
} else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
tail.next = a;
tail.next = b;
return dummy.next;
}Attempts:
2 left
š” Hint
Check how tail.next is assigned after the loop.
ā Incorrect
The code assigns tail.next twice after the loop, so the first assignment is overwritten. This causes some nodes to be lost in the merged list.
ā Predict Output
advancedOutput After Sorting a Longer Linked List
What is the printed linked list after sorting this list using merge sort?
Initial list: 10 -> 5 -> 3 -> 8 -> 2 -> 7 -> null
Initial list: 10 -> 5 -> 3 -> 8 -> 2 -> 7 -> null
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } public class Solution { public static ListNode mergeSort(ListNode head) { if (head == null || head.next == null) return head; ListNode mid = getMiddle(head); ListNode nextToMid = mid.next; mid.next = null; ListNode left = mergeSort(head); ListNode right = mergeSort(nextToMid); return merge(left, right); } private static ListNode getMiddle(ListNode head) { if (head == null) return head; ListNode slow = head, fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } private static ListNode merge(ListNode a, ListNode b) { ListNode dummy = new ListNode(0); ListNode tail = dummy; while (a != null && b != null) { if (a.val <= b.val) { tail.next = a; a = a.next; } else { tail.next = b; b = b.next; } tail = tail.next; } tail.next = (a != null) ? a : b; return dummy.next; } public static void printList(ListNode head) { while (head != null) { System.out.print(head.val); if (head.next != null) System.out.print(" -> "); head = head.next; } System.out.println(" -> null"); } public static void main(String[] args) { ListNode head = new ListNode(10); head.next = new ListNode(5); head.next.next = new ListNode(3); head.next.next.next = new ListNode(8); head.next.next.next.next = new ListNode(2); head.next.next.next.next.next = new ListNode(7); ListNode sorted = mergeSort(head); printList(sorted); } }
Attempts:
2 left
š” Hint
Merge sort sorts the list in ascending order.
ā Incorrect
The merge sort algorithm sorts the list in ascending order, so the output is the sorted list from smallest to largest.
š§ Conceptual
expertTime Complexity of Merge Sort on Linked List
What is the time complexity of merge sort when applied to a linked list with n nodes?
Attempts:
2 left
š” Hint
Consider how many times the list is split and merged.
ā Incorrect
Merge sort splits the list into halves log n times and merges all nodes n times, resulting in O(n log n) time complexity.
