Challenge - 5 Problems
BST Max Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the maximum element in a BST
What is the output of this Rust code that finds the maximum element in a binary search tree?
DSA Rust
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn find_max(&self) -> i32 {
match &self.right {
Some(right_node) => right_node.find_max(),
None => self.value,
}
}
}
fn main() {
let tree = Node {
value: 10,
left: Some(Box::new(Node {
value: 5,
left: None,
right: None,
})),
right: Some(Box::new(Node {
value: 20,
left: None,
right: Some(Box::new(Node {
value: 30,
left: None,
right: None,
})),
})),
};
println!("{}", tree.find_max());
}Attempts:
2 left
💡 Hint
Remember, in a BST, the maximum element is the rightmost node.
✗ Incorrect
The find_max function recursively goes to the right child until it finds a node with no right child. That node's value is the maximum. Here, the rightmost node has value 30.
❓ Predict Output
intermediateOutput of find_max on a single-node BST
What will be printed when find_max is called on a BST with only one node?
DSA Rust
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn find_max(&self) -> i32 {
match &self.right {
Some(right_node) => right_node.find_max(),
None => self.value,
}
}
}
fn main() {
let tree = Node {
value: 42,
left: None,
right: None,
};
println!("{}", tree.find_max());
}Attempts:
2 left
💡 Hint
If there is no right child, the current node's value is the maximum.
✗ Incorrect
Since the tree has only one node with value 42 and no right child, find_max returns 42.
🔧 Debug
advancedIdentify the error in this BST maximum finder
What error will this Rust code produce when trying to find the maximum element in a BST?
DSA Rust
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn find_max(&self) -> i32 {
if self.right.is_some() {
self.right.as_ref().unwrap().find_max()
} else {
self.value
}
}
}
fn main() {
let tree = Node {
value: 15,
left: None,
right: None,
};
println!("{}", tree.find_max());
}Attempts:
2 left
💡 Hint
Check how unwrap() is used on a reference to Option.
✗ Incorrect
Calling unwrap() on &Option> tries to move the Box out of a borrowed reference, which Rust forbids. This causes a compilation error.
🧠 Conceptual
advancedWhy does find_max only check the right subtree in a BST?
In a binary search tree, why is it sufficient to only follow the right child nodes to find the maximum element?
Attempts:
2 left
💡 Hint
Recall the BST property about node values in left and right subtrees.
✗ Incorrect
In a BST, all values in the right subtree are greater than the current node's value, so the maximum must be in the right subtree or the current node if no right child exists.
🚀 Application
expertFind maximum element after inserting nodes in BST
Given the following sequence of insertions into an empty BST: 25, 15, 50, 10, 22, 35, 70, what is the maximum element in the BST?
Attempts:
2 left
💡 Hint
The maximum element is the rightmost node after all insertions.
✗ Incorrect
After inserting all nodes, the rightmost node is 70, which is the maximum element in the BST.
