Challenge - 5 Problems
Merge K Sorted Lists Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of merging 3 sorted lists using min heap
What is the printed state of the merged linked list after merging these 3 sorted lists using a min heap?
DSA Rust
use std::collections::BinaryHeap; use std::cmp::Reverse; #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] struct ListNode { val: i32, next: Option<Box<ListNode>>, } impl ListNode { fn new(val: i32) -> Self { ListNode { val, next: None } } } fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> { let mut heap = BinaryHeap::new(); for (i, list) in lists.into_iter().enumerate() { if let Some(node) = list { heap.push(Reverse((node.val, i, node))); } } let mut dummy = Box::new(ListNode::new(0)); let mut tail = &mut dummy; while let Some(Reverse((val, i, mut node))) = heap.pop() { tail.next = Some(Box::new(ListNode::new(val))); tail = tail.next.as_mut().unwrap(); if let Some(next_node) = node.next.take() { heap.push(Reverse((next_node.val, i, next_node))); } } dummy.next } fn main() { let list1 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 4, next: Some(Box::new(ListNode::new(5))) })) })); let list2 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode::new(4))) })) })); let list3 = Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode::new(6))) })); let merged = merge_k_lists(vec![list1, list2, list3]); let mut current = &merged; while let Some(node) = current { print!("{} -> ", node.val); current = &node.next; } print!("null\n"); }
Attempts:
2 left
💡 Hint
Remember that the min heap always picks the smallest current node from all lists.
✗ Incorrect
The merged list contains all nodes from the three sorted lists in ascending order. The min heap helps pick the smallest node at each step, resulting in the sequence 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6 -> null.
🧠 Conceptual
intermediateWhy use a min heap to merge K sorted lists?
What is the main advantage of using a min heap to merge K sorted linked lists compared to merging them one by one?
Attempts:
2 left
💡 Hint
Think about how the min heap helps pick the smallest element quickly.
✗ Incorrect
Using a min heap allows efficient selection of the smallest node among all current heads of the K lists, reducing the time complexity from O(NK) to O(N log K), where N is total nodes.
🔧 Debug
advancedIdentify the error in this Rust code merging K sorted lists
What error will this Rust code produce when trying to compile?
DSA Rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) -> Option<Box<ListNode>> {
let mut heap = BinaryHeap::new();
for list in lists {
if let Some(node) = list {
heap.push(Reverse((node.val, node)));
}
}
let mut dummy = Box::new(ListNode { val: 0, next: None });
let mut tail = &mut dummy;
while let Some(Reverse((val, mut node))) = heap.pop() {
tail.next = Some(Box::new(ListNode { val, next: None }));
tail = tail.next.as_mut().unwrap();
if let Some(next_node) = node.next.take() {
heap.push(Reverse((next_node.val, next_node)));
}
}
dummy.next
}
Attempts:
2 left
💡 Hint
Check if the tuple pushed into the heap implements the required traits.
✗ Incorrect
BinaryHeap requires its elements to implement Ord. The tuple (i32, Box) does not implement Ord because Box does not implement Ord, causing a compilation error.
📝 Syntax
advancedWhich option correctly uses a min heap to merge K sorted lists in Rust?
Select the option that correctly pushes nodes into a min heap for merging K sorted lists.
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
use std::collections::BinaryHeap;
use std::cmp::Reverse;
fn merge_k_lists(lists: Vec<Option<Box<ListNode>>>) {
let mut heap = BinaryHeap::new();
// Push nodes here
}Attempts:
2 left
💡 Hint
Remember to include an index to break ties and use Reverse for min heap.
✗ Incorrect
Option C correctly pushes a tuple with value, index, and node wrapped in Reverse to create a min heap. Option C misses the index causing Ord issues. Options C and D are invalid syntax or incomplete.
🚀 Application
expertWhat is the time complexity of merging K sorted lists with total N nodes using a min heap?
Given K sorted linked lists with a total of N nodes, what is the time complexity of merging them using a min heap?
Attempts:
2 left
💡 Hint
Consider how many times nodes are pushed and popped from the heap and the heap size.
✗ Incorrect
Each of the N nodes is pushed and popped from a heap of size at most K, so each operation costs O(log K), resulting in O(N log K) time complexity.
