Challenge - 5 Problems
Merge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after merging two sorted linked lists?
Given two sorted linked lists, what is the printed state of the merged linked list?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn merge_two_lists(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
match (l1, l2) {
(Some(mut node1), Some(mut node2)) => {
if node1.val < node2.val {
let next = node1.next.take();
node1.next = merge_two_lists(next, Some(node2));
Some(node1)
} else {
let next = node2.next.take();
node2.next = merge_two_lists(Some(node1), next);
Some(node2)
}
}
(Some(node1), None) => Some(node1),
(None, Some(node2)) => Some(node2),
(None, None) => None,
}
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head;
let mut result = vec![];
while let Some(node) = current {
result.push(node.val);
current = &node.next;
}
println!("{:?}", result);
}
fn main() {
let l1 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode { val: 5, next: None })) })) }));
let l2 = Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 4, next: Some(Box::new(ListNode { val: 6, next: None })) })) }));
let merged = merge_two_lists(l1, l2);
print_list(&merged);
}Attempts:
2 left
💡 Hint
Think about how the merge function compares the head nodes of both lists and recursively merges the rest.
✗ Incorrect
The merge function compares the values at the heads of both lists and picks the smaller one to continue merging. This results in a sorted merged list.
🧠 Conceptual
intermediateWhat is the time complexity of merging two sorted linked lists?
If you merge two sorted linked lists of lengths m and n, what is the time complexity of the merge operation?
Attempts:
2 left
💡 Hint
Consider how many nodes you visit in total during the merge.
✗ Incorrect
Each node from both lists is visited exactly once during the merge, so the time complexity is linear in the total number of nodes.
🔧 Debug
advancedWhat error does this Rust code produce?
Consider this Rust code snippet for merging two sorted linked lists. What error will it produce when compiled?
DSA Rust
fn merge_two_lists(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
match (l1, l2) {
(Some(node1), Some(node2)) => {
if node1.val < node2.val {
node1.next = merge_two_lists(node1.next, Some(node2));
Some(node1)
} else {
node2.next = merge_two_lists(Some(node1), node2.next);
Some(node2)
}
}
(Some(node1), None) => Some(node1),
(None, Some(node2)) => Some(node2),
(None, None) => None,
}
}Attempts:
2 left
💡 Hint
Look at how `node1` and `node2` are used after being matched as immutable bindings.
✗ Incorrect
The variables `node1` and `node2` are immutable by default. Trying to assign to their fields causes a compile error about assigning to an immutable variable.
❓ Predict Output
advancedWhat is the output of this iterative merge function?
Given two sorted linked lists, what is the printed merged list after running this iterative merge function?
DSA Rust
struct ListNode {
val: i32,
next: Option<Box<ListNode>>,
}
fn merge_two_lists_iter(
mut l1: Option<Box<ListNode>>,
mut l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode { val: 0, next: None });
let mut tail = &mut dummy;
while l1.is_some() && l2.is_some() {
if l1.as_ref().unwrap().val < l2.as_ref().unwrap().val {
let next = l1.as_mut().unwrap().next.take();
tail.next = l1;
l1 = next;
} else {
let next = l2.as_mut().unwrap().next.take();
tail.next = l2;
l2 = next;
}
tail = tail.next.as_mut().unwrap();
}
tail.next = if l1.is_some() { l1 } else { l2 };
dummy.next
}
fn print_list(head: &Option<Box<ListNode>>) {
let mut current = head;
let mut result = vec![];
while let Some(node) = current {
result.push(node.val);
current = &node.next;
}
println!("{:?}", result);
}
fn main() {
let l1 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 4, next: None })) }));
let l2 = Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 3, next: None })) }));
let merged = merge_two_lists_iter(l1, l2);
print_list(&merged);
}Attempts:
2 left
💡 Hint
The iterative method picks the smaller node each time and appends it to the merged list.
✗ Incorrect
The function merges nodes in sorted order by comparing heads and appending the smaller one, resulting in a sorted merged list.
🚀 Application
expertHow many nodes are in the merged list after merging these two lists?
List 1 has nodes with values [1, 3, 5, 7, 9]. List 2 has nodes with values [2, 4, 6]. After merging these two sorted linked lists, how many nodes does the merged list contain?
Attempts:
2 left
💡 Hint
Count the total nodes from both lists combined.
✗ Incorrect
The merged list contains all nodes from both lists, so total nodes = 5 + 3 = 8.
