Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a min heap using Rust's BinaryHeap with reversed order.
DSA Rust
let mut heap = std::collections::BinaryHeap::new(); heap.[1](5);
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push to add elements.
Trying to create a new heap inside the push call.
✗ Incorrect
The push method adds an element to the heap. Here, we add the value 5 to the min heap.
2fill in blank
mediumComplete the code to pop the smallest element from the min heap.
DSA Rust
if let Some(smallest) = heap.[1]() { println!("Smallest: {}", smallest); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using peek instead of pop, which does not remove the element.
Using push instead of pop.
✗ Incorrect
The pop method removes and returns the smallest element from the min heap.
3fill in blank
hardFix the error in the code to correctly compare elements in the min heap by reversing the order.
DSA Rust
use std::cmp::[1]; struct Wrapper(i32); impl PartialEq for Wrapper { fn eq(&self, other: &Self) -> bool { self.0 == other.0 } } impl Eq for Wrapper {} impl PartialOrd for Wrapper { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { Some(self.cmp(other)) } } impl Ord for Wrapper { fn cmp(&self, other: &Self) -> std::cmp::Ordering { other.0.[2](self.0) } }
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong trait or enum.
Not reversing the order in cmp method.
✗ Incorrect
The code uses std::cmp::Ordering for comparison and reverses the order by calling other.0.cmp(self.0) to create a min heap behavior.
4fill in blank
hardFill both blanks to correctly pop the smallest node and push the next node from the list.
DSA Rust
while let Some(Wrapper(node)) = heap.[1]() { if node.next[2] { heap.push(Wrapper(node.next.unwrap())); } // process node.val }
Attempts:
3 left
💡 Hint
Common Mistakes
Using peek instead of pop to remove the node.
Checking next == None instead of is_some().
✗ Incorrect
We pop the smallest node from the heap and check if the next node exists using is_some() before pushing it.
5fill in blank
hardFill both blanks to build the merged list by popping from the heap and linking nodes.
DSA Rust
let mut dummy = ListNode::new(0); let mut tail = &mut dummy; while let Some(Wrapper(node)) = heap.[1]() { tail.next = Some(node); tail = tail.next.as_mut().unwrap(); if tail.next[2] { heap.push(Wrapper(tail.next.unwrap())); } } return dummy.next;
Attempts:
3 left
💡 Hint
Common Mistakes
Using peek instead of pop to get nodes.
Checking next == None instead of is_some().
✗ Incorrect
We pop the smallest node from the heap, link it to the merged list, then if the next node exists (is_some()), we push it to the heap.
