Challenge - 5 Problems
BST Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Inorder Traversal on a BST
What is the output of the inorder traversal of this BST after inserting the values 10, 5, 15, 3, 7 in that order?
DSA Rust
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn new(value: i32) -> Self {
Node { value, left: None, right: None }
}
fn insert(&mut self, val: i32) {
if val < self.value {
match self.left {
Some(ref mut left_node) => left_node.insert(val),
None => self.left = Some(Box::new(Node::new(val))),
}
} else {
match self.right {
Some(ref mut right_node) => right_node.insert(val),
None => self.right = Some(Box::new(Node::new(val))),
}
}
}
fn inorder(&self, vals: &mut Vec<i32>) {
if let Some(ref left_node) = self.left {
left_node.inorder(vals);
}
vals.push(self.value);
if let Some(ref right_node) = self.right {
right_node.inorder(vals);
}
}
}
fn main() {
let mut root = Node::new(10);
root.insert(5);
root.insert(15);
root.insert(3);
root.insert(7);
let mut result = Vec::new();
root.inorder(&mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember that inorder traversal of a BST visits nodes in ascending order.
✗ Incorrect
Inorder traversal visits the left subtree, then the node, then the right subtree. For a BST, this results in sorted order.
🧠 Conceptual
intermediateWhy Does BST Property Matter for Search?
Why is the BST property important for efficient searching in a binary search tree?
Attempts:
2 left
💡 Hint
Think about how the tree structure helps decide which side to search next.
✗ Incorrect
The BST property lets us decide to go left or right at each node based on comparison, enabling efficient binary search.
🔧 Debug
advancedIdentify the Bug Breaking BST Property
Which option shows a bug that breaks the BST property when inserting a new value?
DSA Rust
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn insert(&mut self, val: i32) {
if val <= self.value {
match self.left {
Some(ref mut left_node) => left_node.insert(val),
None => self.left = Some(Box::new(Node { value: val, left: None, right: None })),
}
} else {
match self.right {
Some(ref mut right_node) => right_node.insert(val),
None => self.right = Some(Box::new(Node { value: val, left: None, right: None })),
}
}
}
}Attempts:
2 left
💡 Hint
Check which option causes values to be inserted incorrectly breaking BST ordering.
✗ Incorrect
Option B inserts larger values on the left subtree, which breaks the BST property that left subtree values must be smaller.
❓ Predict Output
advancedResult of Deleting a Node with Two Children in BST
Given a BST with nodes 20, 10, 30, 5, 15, 25, 35, what is the inorder traversal after deleting node 20?
DSA Rust
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn new(value: i32) -> Self {
Node { value, left: None, right: None }
}
fn insert(&mut self, val: i32) {
if val < self.value {
match self.left {
Some(ref mut left_node) => left_node.insert(val),
None => self.left = Some(Box::new(Node::new(val))),
}
} else {
match self.right {
Some(ref mut right_node) => right_node.insert(val),
None => self.right = Some(Box::new(Node::new(val))),
}
}
}
fn inorder(&self, vals: &mut Vec<i32>) {
if let Some(ref left_node) = self.left {
left_node.inorder(vals);
}
vals.push(self.value);
if let Some(ref right_node) = self.right {
right_node.inorder(vals);
}
}
fn find_min(&self) -> i32 {
match &self.left {
Some(left_node) => left_node.find_min(),
None => self.value,
}
}
fn delete(self: Box<Self>, val: i32) -> Option<Box<Node>> {
if val < self.value {
let left = match self.left {
Some(left_node) => left_node.delete(val),
None => None,
};
Some(Box::new(Node { value: self.value, left, right: self.right }))
} else if val > self.value {
let right = match self.right {
Some(right_node) => right_node.delete(val),
None => None,
};
Some(Box::new(Node { value: self.value, left: self.left, right }))
} else {
if self.left.is_none() {
return self.right;
} else if self.right.is_none() {
return self.left;
} else {
let min_val = self.right.as_ref().unwrap().find_min();
let right = self.right.unwrap().delete(min_val);
Some(Box::new(Node { value: min_val, left: self.left, right }))
}
}
}
}
fn main() {
let mut root = Node::new(20);
root.insert(10);
root.insert(30);
root.insert(5);
root.insert(15);
root.insert(25);
root.insert(35);
let root_box = Box::new(root);
let new_root = root_box.delete(20).unwrap();
let mut result = Vec::new();
new_root.inorder(&mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
When deleting a node with two children, replace it with the smallest value in the right subtree.
✗ Incorrect
Deleting node 20 replaces it with 25 (smallest in right subtree). The inorder traversal reflects this replacement.
🧠 Conceptual
expertWhy Can BST Become Inefficient Without Balancing?
Why can a binary search tree become inefficient if it is not balanced?
Attempts:
2 left
💡 Hint
Think about the shape of the tree and how it affects search steps.
✗ Incorrect
Without balancing, the tree can degenerate into a chain, making search linear time instead of logarithmic.
