Challenge - 5 Problems
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kth Smallest Element Extraction
What is the output of the following Rust code that finds the 3rd smallest element using a min heap?
DSA Rust
use std::collections::BinaryHeap;
fn kth_smallest(nums: &[i32], k: usize) -> i32 {
let mut min_heap = BinaryHeap::new();
for &num in nums {
min_heap.push(-num); // Using max heap as min heap by negating
}
let mut result = 0;
for _ in 0..k {
result = -min_heap.pop().unwrap();
}
result
}
fn main() {
let nums = [7, 10, 4, 3, 20, 15];
let k = 3;
println!("{}", kth_smallest(&nums, k));
}Attempts:
2 left
💡 Hint
Remember that the min heap is simulated by negating values in Rust's max heap.
✗ Incorrect
The code negates all numbers to use Rust's max heap as a min heap. Popping three times gives the 3rd smallest element, which is 7.
🧠 Conceptual
intermediateUnderstanding Min Heap Behavior in Kth Smallest Element
Which statement correctly describes how a min heap helps find the kth smallest element in an unsorted array?
Attempts:
2 left
💡 Hint
Think about how popping from a min heap works.
✗ Incorrect
A min heap keeps the smallest element at the root. By inserting all elements and popping k times, the kth smallest element is obtained.
🔧 Debug
advancedIdentify the Error in Kth Smallest Element Code
What error will the following Rust code produce when trying to find the 1st smallest element?
DSA Rust
use std::collections::BinaryHeap;
fn kth_smallest(nums: &[i32], k: usize) -> i32 {
let mut min_heap = BinaryHeap::new();
for &num in nums {
min_heap.push(num); // Incorrect: pushing positive values
}
let mut result = 0;
for _ in 0..k {
result = min_heap.pop().unwrap();
}
result
}
fn main() {
let nums = [5, 3, 8, 1, 2];
let k = 1;
println!("{}", kth_smallest(&nums, k));
}Attempts:
2 left
💡 Hint
Consider how Rust's BinaryHeap orders elements by default.
✗ Incorrect
Rust's BinaryHeap is a max heap by default. Pushing positive values and popping returns largest elements first. So popping once returns the largest (8), not the smallest.
🚀 Application
advancedResulting Heap State After Insertions
After inserting the elements [9, 4, 7, 1, 3] into a min heap, what is the printed state of the heap array (assuming a binary heap stored in an array)?
DSA Rust
let mut min_heap = vec![]; let elements = [9, 4, 7, 1, 3]; for &el in &elements { min_heap.push(el); // Assume heapify-up is called here to maintain min heap property } println!("{:?}", min_heap);
Attempts:
2 left
💡 Hint
Remember min heap property: parent is smaller than children.
✗ Incorrect
After inserting all elements with heapify-up, the min heap array is [1, 3, 7, 9, 4].
❓ Predict Output
expertOutput of Kth Smallest Element Using Min Heap with Duplicates
What is the output of the following Rust code that finds the 5th smallest element in an array with duplicates using a min heap?
DSA Rust
use std::collections::BinaryHeap;
fn kth_smallest(nums: &[i32], k: usize) -> i32 {
let mut min_heap = BinaryHeap::new();
for &num in nums {
min_heap.push(-num);
}
let mut result = 0;
for _ in 0..k {
result = -min_heap.pop().unwrap();
}
result
}
fn main() {
let nums = [2, 1, 2, 3, 4, 2, 5];
let k = 5;
println!("{}", kth_smallest(&nums, k));
}Attempts:
2 left
💡 Hint
Duplicates count as separate elements when popping from the heap.
✗ Incorrect
The sorted array is [1, 2, 2, 2, 3, 4, 5]. The 5th smallest element is 3.
