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 sorting it using merge sort?
Initial linked list: 4 -> 2 -> 1 -> 3 -> null
Initial linked list: 4 -> 2 -> 1 -> 3 -> null
DSA Javascript
class ListNode { constructor(val, next = null) { this.val = val; this.next = next; } } function mergeSort(head) { if (!head || !head.next) return head; // Find middle let slow = head, fast = head.next; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } let mid = slow.next; slow.next = null; let left = mergeSort(head); let right = mergeSort(mid); return merge(left, right); } function merge(l1, l2) { let dummy = new ListNode(0); let current = dummy; while (l1 && l2) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } current = current.next; } current.next = l1 || l2; return dummy.next; } // Create linked list 4 -> 2 -> 1 -> 3 -> null let head = new ListNode(4, new ListNode(2, new ListNode(1, new ListNode(3)))); // Sort head = mergeSort(head); // Print sorted list let result = []; while (head) { result.push(head.val); head = head.next; } console.log(result.join(' -> ') + ' -> null');
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 linked list is 1 -> 2 -> 3 -> 4 -> null.
❓ Predict Output
intermediateResult of Merging Two Sorted Linked Lists
What is the printed state of the linked list after merging these two sorted lists?
List 1: 1 -> 3 -> 5 -> null
List 2: 2 -> 4 -> 6 -> null
List 1: 1 -> 3 -> 5 -> null
List 2: 2 -> 4 -> 6 -> null
DSA Javascript
class ListNode { constructor(val, next = null) { this.val = val; this.next = next; } } function merge(l1, l2) { let dummy = new ListNode(0); let current = dummy; while (l1 && l2) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } current = current.next; } current.next = l1 || l2; return dummy.next; } // Create lists let l1 = new ListNode(1, new ListNode(3, new ListNode(5))); let l2 = new ListNode(2, new ListNode(4, new ListNode(6))); // Merge let merged = merge(l1, l2); // Print merged list let result = []; while (merged) { result.push(merged.val); merged = merged.next; } console.log(result.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Merging two sorted lists results in a sorted combined list.
✗ Incorrect
The merge function compares nodes from both lists and links them in ascending order, producing a fully sorted merged list.
🧠 Conceptual
advancedWhy 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 Sort Implementation
What is the bug in this merge sort code for linked lists?
Code snippet:
function mergeSort(head) { if (!head || !head.next) return head; let slow = head, fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } let mid = slow.next; slow.next = null; let left = mergeSort(head); let right = mergeSort(mid); return merge(left, right); }
Code snippet:
function mergeSort(head) { if (!head || !head.next) return head; let slow = head, fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } let mid = slow.next; slow.next = null; let left = mergeSort(head); let right = mergeSort(mid); return merge(left, right); }
Attempts:
2 left
💡 Hint
Check how the middle node is found using slow and fast pointers.
✗ Incorrect
Starting fast at head.next ensures the slow pointer stops at the node before the middle, correctly splitting the list. Starting fast at head causes incorrect splitting for even-length lists.
🚀 Application
expertNumber of Recursive Calls in Merge Sort on Linked List
Given a linked list of 8 nodes, how many total recursive calls to mergeSort are made during the full sort process?
Attempts:
2 left
💡 Hint
Think about how merge sort splits the list into halves recursively until single nodes.
✗ Incorrect
Merge sort splits the list into halves until single nodes remain. For 8 nodes, the recursion tree has 15 calls (7 internal nodes + 8 leaves).
