Challenge - 5 Problems
BST Floor and Ceil Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the floor value for 15 in this BST?
Given the BST constructed by inserting these values in order: [10, 5, 20, 12, 30], what is the floor of 15?
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
};
fn floor(root: &Option<Box<Node>>, key: i32) -> Option<i32> {
match root {
None => None,
Some(node) => {
if node.val == key {
Some(node.val)
} else if node.val > key {
floor(&node.left, key)
} else {
let right_floor = floor(&node.right, key);
match right_floor {
Some(v) if v <= key => Some(v),
_ => Some(node.val),
}
}
}
}
}
fn main() {
let root = Some(Box::new(Node {
val: 10,
left: Some(Box::new(Node {
val: 5,
left: None,
right: None,
})),
right: Some(Box::new(Node {
val: 20,
left: Some(Box::new(Node {
val: 12,
left: None,
right: None,
})),
right: Some(Box::new(Node {
val: 30,
left: None,
right: None,
})),
})),
}));
let result = floor(&root, 15);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Floor is the greatest value less than or equal to the key.
✗ Incorrect
The floor of 15 is 12 because 12 is the greatest value in the BST less than or equal to 15.
❓ Predict Output
intermediateWhat is the ceil value for 13 in this BST?
Given the BST constructed by inserting these values in order: [8, 4, 12, 10, 14], what is the ceil of 13?
DSA Rust
struct Node {
val: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
};
fn ceil(root: &Option<Box<Node>>, key: i32) -> Option<i32> {
match root {
None => None,
Some(node) => {
if node.val == key {
Some(node.val)
} else if node.val < key {
ceil(&node.right, key)
} else {
let left_ceil = ceil(&node.left, key);
match left_ceil {
Some(v) if v >= key => Some(v),
_ => Some(node.val),
}
}
}
}
}
fn main() {
let root = Some(Box::new(Node {
val: 8,
left: Some(Box::new(Node {
val: 4,
left: None,
right: None,
})),
right: Some(Box::new(Node {
val: 12,
left: Some(Box::new(Node {
val: 10,
left: None,
right: None,
})),
right: Some(Box::new(Node {
val: 14,
left: None,
right: None,
})),
})),
}));
let result = ceil(&root, 13);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Ceil is the smallest value greater than or equal to the key.
✗ Incorrect
The ceil of 13 is 14 because 14 is the smallest value in the BST greater than or equal to 13.
🔧 Debug
advancedWhy does this floor function return incorrect results?
Consider this Rust function to find floor in BST. It sometimes returns None incorrectly. Identify the bug.
DSA Rust
fn floor(root: &Option<Box<Node>>, key: i32) -> Option<i32> {
match root {
None => None,
Some(node) => {
if node.val == key {
Some(node.val)
} else if node.val > key {
floor(&node.left, key)
} else {
let right_floor = floor(&node.right, key);
if right_floor.is_some() {
right_floor
} else {
Some(node.val)
}
}
}
}
}Attempts:
2 left
💡 Hint
Check the condition on right_floor before returning it.
✗ Incorrect
The function returns right_floor if it exists without verifying if right_floor <= key. This can cause returning a value greater than key, which is incorrect for floor.
🧠 Conceptual
advancedWhat is the time complexity of finding floor or ceil in a balanced BST?
Assuming a balanced BST with n nodes, what is the time complexity to find floor or ceil of a key?
Attempts:
2 left
💡 Hint
Think about the height of a balanced BST.
✗ Incorrect
In a balanced BST, the height is about log n, so searching for floor or ceil takes O(log n) time.
🚀 Application
expertAfter inserting [25, 15, 50, 10, 22, 35, 70] into an empty BST, what is the ceil of 23?
Insert the values in order into an empty BST. Then find the ceil of 23.
Attempts:
2 left
💡 Hint
Ceil is the smallest value >= key. Trace the path for 23.
✗ Incorrect
The ceil of 23 is 25 because 25 is the smallest value in the BST greater than or equal to 23.
