Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a value into the BST.
DSA Rust
fn insert(root: &mut Option<Box<TreeNode>>, val: i32) {
match root {
None => *root = Some(Box::new(TreeNode { val, left: None, right: None })),
Some(node) => {
if val < node.val {
insert(&mut node.left, [1]);
} else {
insert(&mut node.right, val);
}
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing a modified value instead of the original 'val'.
Using node.val instead of val.
✗ Incorrect
We must pass the value to insert unchanged to the recursive call.
2fill in blank
mediumComplete the code to find the floor value in BST for a given key.
DSA Rust
fn floor(root: &Option<Box<TreeNode>>, 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 {
[1]
}
}
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of current node's value.
Returning key or right child's value incorrectly.
✗ Incorrect
If right subtree has no floor, current node's value is the floor.
3fill in blank
hardFix the error in the ceil function to correctly find the ceil value in BST.
DSA Rust
fn ceil(root: &Option<Box<TreeNode>>, 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);
if left_ceil.is_some() {
left_ceil
} else {
[1]
}
}
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of current node's value.
Returning left child's value incorrectly.
✗ Incorrect
If left subtree has no ceil, current node's value is the ceil.
4fill in blank
hardFill both blanks to complete the function that finds floor and ceil in BST.
DSA Rust
fn floor_ceil(root: &Option<Box<TreeNode>>, key: i32) -> (Option<i32>, Option<i32>) {
match root {
None => (None, None),
Some(node) => {
if node.val == key {
(Some(node.val), Some(node.val))
} else if node.val > key {
let (floor, ceil) = floor_ceil(&node.left, key);
(floor, [1])
} else {
let (floor, ceil) = floor_ceil(&node.right, key);
([2], ceil)
}
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of node.val for floor or ceil.
Mixing floor and ceil values.
✗ Incorrect
When node.val > key, ceil is node.val; when node.val < key, floor is node.val.
5fill in blank
hardFill all three blanks to complete the function that prints floor and ceil for a list of keys.
DSA Rust
fn print_floor_ceil(root: &Option<Box<TreeNode>>, keys: &[i32]) {
for &key in keys {
let (floor, ceil) = floor_ceil(root, key);
println!("Key: {}, Floor: {:?}, Ceil: {:?}", [1], [2], [3]);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Printing Option types directly without unwrap_or.
Mixing order of floor and ceil in print.
✗ Incorrect
Print key, floor (or -1 if None), and ceil (or -1 if None) for each key.
