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 Function Call
What is the output of the following Rust function call to find the 3rd smallest element in the BST?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option<Rc<RefCell<TreeNode>>>, pub right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { #[inline] pub fn new(val: i32) -> Self { TreeNode { val, left: None, right: None, } } } fn kth_smallest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 { fn inorder(node: &Option<Rc<RefCell<TreeNode>>>, k: &mut i32, result: &mut i32) { if let Some(n) = node { inorder(&n.borrow().left, k, result); *k -= 1; if *k == 0 { *result = n.borrow().val; return; } inorder(&n.borrow().right, k, result); } } let mut k_mut = k; let mut result = -1; inorder(&root, &mut k_mut, &mut result); result } fn main() { let root = Rc::new(RefCell::new(TreeNode::new(5))); root.borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(3)))); root.borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(6)))); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(2)))); root.borrow_mut().left.as_ref().unwrap().borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(4)))); root.borrow_mut().left.as_ref().unwrap().borrow_mut().left.as_ref().unwrap().borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(1)))); println!("{}", kth_smallest(Some(root), 3)); }
Attempts:
2 left
💡 Hint
Remember that inorder traversal of BST gives sorted order.
✗ Incorrect
The inorder traversal of the BST visits nodes in ascending order: 1, 2, 3, 4, 5, 6. The 3rd smallest element is 3.
🧠 Conceptual
intermediateUnderstanding Inorder Traversal in BST
Why does inorder traversal help find the kth smallest element in a BST?
Attempts:
2 left
💡 Hint
Think about the BST property and how inorder traversal works.
✗ Incorrect
Inorder traversal visits left subtree, then node, then right subtree, which for BST means visiting nodes in ascending order.
🔧 Debug
advancedIdentify the Error in Kth Smallest Implementation
What error will this Rust code produce when trying to find the kth smallest element?
DSA Rust
fn kth_smallest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 {
let mut count = 0;
let mut result = -1;
fn inorder(node: &Option<Rc<RefCell<TreeNode>>>, k: i32, count: &mut i32, result: &mut i32) {
if let Some(n) = node {
inorder(&n.borrow().left, k, count, result);
*count += 1;
if *count == k {
*result = n.borrow().val;
return;
}
inorder(&n.borrow().right, k, count, result);
}
}
inorder(&root, k, &mut count, &mut result);
result
}Attempts:
2 left
💡 Hint
Check if the recursion stops after finding the kth element.
✗ Incorrect
The recursive calls continue even after kth element is found because return only exits current call, not all recursion.
❓ Predict Output
advancedOutput of Kth Smallest with k Larger Than Node Count
What is the output of the function call kth_smallest(root, 10) on the BST below?
BST structure:
3
/ \
1 4
\
2
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option<Rc<RefCell<TreeNode>>>, pub right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { #[inline] pub fn new(val: i32) -> Self { TreeNode { val, left: None, right: None, } } } fn kth_smallest(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 { fn inorder(node: &Option<Rc<RefCell<TreeNode>>>, k: &mut i32, result: &mut i32) { if let Some(n) = node { inorder(&n.borrow().left, k, result); *k -= 1; if *k == 0 { *result = n.borrow().val; return; } inorder(&n.borrow().right, k, result); } } let mut k_mut = k; let mut result = -1; inorder(&root, &mut k_mut, &mut result); result } fn main() { let root = Rc::new(RefCell::new(TreeNode::new(3))); root.borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(1)))); root.borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(4)))); root.borrow_mut().left.as_ref().unwrap().borrow_mut().right = Some(Rc::new(RefCell::new(TreeNode::new(2)))); println!("{}", kth_smallest(Some(root), 10)); }
Attempts:
2 left
💡 Hint
What happens if k is larger than the number of nodes?
✗ Incorrect
The function initializes result to -1 and only updates it if kth element is found. Since k=10 is larger than node count=4, result remains -1.
🚀 Application
expertOptimizing Kth Smallest Element Search in BST
Which data structure modification allows finding the kth smallest element in O(log n) time on average in a BST?
Attempts:
2 left
💡 Hint
Think about how to avoid full traversal each time.
✗ Incorrect
By storing the size of left subtree at each node, we can decide whether to go left, right, or return current node in O(log n) time.
