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 C++
struct Node {
int val;
Node* next;
Node(int x) : val(x), next(nullptr) {}
};
Node* merge(Node* l1, Node* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
l1->next = merge(l1->next, l2);
return l1;
} else {
l2->next = merge(l1, l2->next);
return l2;
}
}
Node* getMid(Node* head) {
Node* slow = head;
Node* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
Node* mergeSort(Node* head) {
if (!head || !head->next) return head;
Node* mid = getMid(head);
Node* rightHead = mid->next;
mid->next = nullptr;
Node* left = mergeSort(head);
Node* right = mergeSort(rightHead);
return merge(left, right);
}
void printList(Node* head) {
while (head) {
std::cout << head->val;
if (head->next) std::cout << " -> ";
head = head->next;
}
std::cout << " -> null" << std::endl;
}
int main() {
Node* head = new Node(4);
head->next = new Node(2);
head->next->next = new Node(1);
head->next->next->next = new Node(3);
head = mergeSort(head);
printList(head);
return 0;
}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
intermediateUnderstanding the Midpoint in Merge Sort for Linked List
In the merge sort algorithm for linked lists, what is the purpose of the 'getMid' function?
Attempts:
2 left
💡 Hint
Think about how merge sort divides the list.
✗ Incorrect
The 'getMid' function uses slow and fast pointers to find the middle node, which helps split the list into two halves for recursive sorting.
🔧 Debug
advancedIdentify the Bug in Merge Function
What error will occur if the merge function is implemented as below?
Node* merge(Node* l1, Node* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val <= l2->val) {
l1->next = merge(l1->next, l2);
return l1;
} else {
l2->next = merge(l1, l2->next);
return l2;
}
}
Assuming the input lists are sorted and non-empty.
DSA C++
Node* merge(Node* l1, Node* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val <= l2->val) {
l1->next = merge(l1->next, l2);
return l1;
} else {
l2->next = merge(l1, l2->next);
return l2;
}
}Attempts:
2 left
💡 Hint
Check how equal values are handled in recursion.
✗ Incorrect
The merge function correctly handles equal values by choosing l1 when values are equal, so no infinite recursion occurs. It merges sorted lists properly.
❓ Predict Output
advancedOutput After Sorting a Linked List with Duplicate Values
What is the printed state of the linked list after applying merge sort on the list 5 -> 3 -> 3 -> 7 -> 2 -> null?
DSA C++
struct Node {
int val;
Node* next;
Node(int x) : val(x), next(nullptr) {}
};
Node* merge(Node* l1, Node* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
l1->next = merge(l1->next, l2);
return l1;
} else {
l2->next = merge(l1, l2->next);
return l2;
}
}
Node* getMid(Node* head) {
Node* slow = head;
Node* fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
Node* mergeSort(Node* head) {
if (!head || !head->next) return head;
Node* mid = getMid(head);
Node* rightHead = mid->next;
mid->next = nullptr;
Node* left = mergeSort(head);
Node* right = mergeSort(rightHead);
return merge(left, right);
}
void printList(Node* head) {
while (head) {
std::cout << head->val;
if (head->next) std::cout << " -> ";
head = head->next;
}
std::cout << " -> null" << std::endl;
}
int main() {
Node* head = new Node(5);
head->next = new Node(3);
head->next->next = new Node(3);
head->next->next->next = new Node(7);
head->next->next->next->next = new Node(2);
head = mergeSort(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Duplicates should be sorted and appear consecutively.
✗ Incorrect
Merge sort sorts the list including duplicates. The sorted list is 2 -> 3 -> 3 -> 5 -> 7 -> null.
🚀 Application
expertTime Complexity of Merge Sort on Linked List
What is the time complexity of merge sort when applied to a linked list of 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 per level, resulting in O(n log n) time complexity.
