Challenge - 5 Problems
BST Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BST Validation Function
What is the output of the following Rust code that checks if a binary tree is a valid BST?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { fn helper(node: Option<Rc<RefCell<TreeNode>>>, min: Option<i32>, max: Option<i32>) -> bool { match node { None => true, Some(n) => { let n = n.borrow(); if let Some(min_val) = min { if n.val <= min_val { return false; } } if let Some(max_val) = max { if n.val >= max_val { return false; } } helper(n.left.clone(), min, Some(n.val)) && helper(n.right.clone(), Some(n.val), max) } } } helper(root, None, None) } fn main() { let node1 = Rc::new(RefCell::new(TreeNode { val: 1, left: None, right: None })); let node3 = Rc::new(RefCell::new(TreeNode { val: 3, left: None, right: None })); let node2 = Rc::new(RefCell::new(TreeNode { val: 2, left: Some(node1), right: Some(node3) })); println!("{}", is_valid_bst(Some(node2))); }
Attempts:
2 left
💡 Hint
Think about the BST property: left subtree values < node value < right subtree values.
✗ Incorrect
The tree constructed is a valid BST with root 2, left child 1, and right child 3. The function correctly checks the BST property recursively and returns true.
❓ Predict Output
intermediateOutput of BST Validation with Invalid Tree
What is the output of the following Rust code that checks if a binary tree is a valid BST?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { fn helper(node: Option<Rc<RefCell<TreeNode>>>, min: Option<i32>, max: Option<i32>) -> bool { match node { None => true, Some(n) => { let n = n.borrow(); if let Some(min_val) = min { if n.val <= min_val { return false; } } if let Some(max_val) = max { if n.val >= max_val { return false; } } helper(n.left.clone(), min, Some(n.val)) && helper(n.right.clone(), Some(n.val), max) } } } helper(root, None, None) } fn main() { let node1 = Rc::new(RefCell::new(TreeNode { val: 1, left: None, right: None })); let node4 = Rc::new(RefCell::new(TreeNode { val: 4, left: None, right: None })); let node3 = Rc::new(RefCell::new(TreeNode { val: 3, left: Some(node4), right: None })); let node2 = Rc::new(RefCell::new(TreeNode { val: 2, left: Some(node1), right: Some(node3) })); println!("{}", is_valid_bst(Some(node2))); }
Attempts:
2 left
💡 Hint
Check if the right child of node 3 violates BST rules.
✗ Incorrect
Node 4 is in the left subtree of node 3 but has a value greater than 3, violating BST property. So the function returns false.
🔧 Debug
advancedIdentify the Bug in BST Validation Code
The following Rust function is intended to check if a binary tree is a valid BST. What is the bug causing incorrect results?
DSA Rust
fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { fn helper(node: Option<Rc<RefCell<TreeNode>>>, min: Option<i32>, max: Option<i32>) -> bool { match node { None => true, Some(n) => { let n = n.borrow(); if let Some(min_val) = min { if n.val < min_val { return false; } } if let Some(max_val) = max { if n.val > max_val { return false; } } helper(n.left.clone(), min, Some(n.val)) && helper(n.right.clone(), Some(n.val), max) } } } helper(root, None, None) }
Attempts:
2 left
💡 Hint
Think about whether equal values are allowed in BST and how comparisons are made.
✗ Incorrect
The bug is that the code uses < and > instead of <= and >=, so it allows equal values which violates strict BST property. Using <= and >= fixes this.
🧠 Conceptual
advancedWhy Use Min and Max Bounds in BST Validation?
Why does the BST validation function pass min and max bounds during recursion instead of just comparing child nodes to their parent?
Attempts:
2 left
💡 Hint
Think about the BST property and how it applies to all nodes in the subtree.
✗ Incorrect
BST property requires all nodes in left subtree to be less than the root and all nodes in right subtree to be greater than the root, so bounds from ancestors must be tracked.
🚀 Application
expertResult of Inserting Nodes and Validating BST
Given the following Rust code that inserts nodes into a BST and then validates it, what is the printed output?
DSA Rust
use std::rc::Rc; use std::cell::RefCell; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } fn insert(root: &mut Option<Rc<RefCell<TreeNode>>>, val: i32) { match root { None => { *root = Some(Rc::new(RefCell::new(TreeNode { val, left: None, right: None }))); } Some(node) => { let mut n = node.borrow_mut(); if val < n.val { insert(&mut n.left, val); } else { insert(&mut n.right, val); } } } } fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { fn helper(node: Option<Rc<RefCell<TreeNode>>>, min: Option<i32>, max: Option<i32>) -> bool { match node { None => true, Some(n) => { let n = n.borrow(); if let Some(min_val) = min { if n.val <= min_val { return false; } } if let Some(max_val) = max { if n.val >= max_val { return false; } } helper(n.left.clone(), min, Some(n.val)) && helper(n.right.clone(), Some(n.val), max) } } } helper(root, None, None) } fn main() { let mut root = None; insert(&mut root, 10); insert(&mut root, 5); insert(&mut root, 15); insert(&mut root, 6); insert(&mut root, 20); insert(&mut root, 4); println!("{}", is_valid_bst(root)); }
Attempts:
2 left
💡 Hint
Check if the insert function maintains BST property and if validation matches.
✗ Incorrect
The insert function correctly places nodes maintaining BST property. The validation confirms the tree is valid, so output is true.
