Challenge - 5 Problems
Two Sum BST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Two Sum BST check with target 9
What is the output of the following Rust code that checks if there exist two nodes in the BST that sum to 9?
DSA Rust
use std::collections::HashSet; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Box<TreeNode>>, right: Option<Box<TreeNode>>, } impl TreeNode { fn new(val: i32) -> Self { TreeNode { val, left: None, right: None } } } fn find_target(root: &Option<Box<TreeNode>>, k: i32, set: &mut HashSet<i32>) -> bool { match root { None => false, Some(node) => { if set.contains(&(k - node.val)) { true } else { set.insert(node.val); find_target(&node.left, k, set) || find_target(&node.right, k, set) } } } } fn main() { let mut root = Box::new(TreeNode::new(5)); root.left = Some(Box::new(TreeNode::new(3))); root.right = Some(Box::new(TreeNode::new(6))); root.left.as_mut().unwrap().left = Some(Box::new(TreeNode::new(2))); root.left.as_mut().unwrap().right = Some(Box::new(TreeNode::new(4))); root.right.as_mut().unwrap().right = Some(Box::new(TreeNode::new(7))); let mut set = HashSet::new(); let result = find_target(&Some(root), 9, &mut set); println!("{}", result); }
Attempts:
2 left
💡 Hint
Think about pairs of nodes that add up to 9 in the BST.
✗ Incorrect
The BST contains nodes with values 2,3,4,5,6,7. The pair (2,7) sums to 9, so the function returns true.
🧠 Conceptual
intermediateUnderstanding Two Sum in BST approach
Which data structure is most suitable to efficiently check if the complement of a node's value exists while traversing a BST for the Two Sum problem?
Attempts:
2 left
💡 Hint
Think about fast membership checking.
✗ Incorrect
HashSet provides O(1) average time complexity for membership checks, making it ideal to check complements during traversal.
❓ Predict Output
advancedOutput of Two Sum BST check with target 30
What is the output of the following Rust code that checks if there exist two nodes in the BST that sum to 30?
DSA Rust
use std::collections::HashSet; #[derive(Debug)] struct TreeNode { val: i32, left: Option<Box<TreeNode>>, right: Option<Box<TreeNode>>, } impl TreeNode { fn new(val: i32) -> Self { TreeNode { val, left: None, right: None } } } fn find_target(root: &Option<Box<TreeNode>>, k: i32, set: &mut HashSet<i32>) -> bool { match root { None => false, Some(node) => { if set.contains(&(k - node.val)) { true } else { set.insert(node.val); find_target(&node.left, k, set) || find_target(&node.right, k, set) } } } } fn main() { let mut root = Box::new(TreeNode::new(10)); root.left = Some(Box::new(TreeNode::new(5))); root.right = Some(Box::new(TreeNode::new(15))); root.left.as_mut().unwrap().left = Some(Box::new(TreeNode::new(3))); root.left.as_mut().unwrap().right = Some(Box::new(TreeNode::new(7))); root.right.as_mut().unwrap().right = Some(Box::new(TreeNode::new(18))); let mut set = HashSet::new(); let result = find_target(&Some(root), 30, &mut set); println!("{}", result); }
Attempts:
2 left
💡 Hint
Check if any two nodes sum to 30.
✗ Incorrect
The BST nodes are 3,5,7,10,15,18. No two nodes sum to 30, so output is false.
🔧 Debug
advancedIdentify the error in Two Sum BST function
What error will this Rust code produce when trying to compile?
DSA Rust
use std::collections::HashSet;
struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn find_target(root: &Option<Box<TreeNode>>, k: i32, set: &mut HashSet<i32>) -> bool {
match root {
None => false,
Some(node) => {
if set.contains(&(k - node.val)) {
true
} else {
set.insert(node.val);
find_target(&node.left, k, set) || find_target(&node.right, k, set)
}
}
}
}
fn main() {
let root = Some(Box::new(TreeNode { val: 1, left: None, right: None }));
let mut set = HashSet::new();
println!("{}", find_target(&root, 2, &mut set));
}Attempts:
2 left
💡 Hint
Look at the argument passed to set.contains.
✗ Incorrect
set.contains expects a reference to i32 (&i32), but k - node.val is an i32 value, causing a mismatched types error.
🚀 Application
expertNumber of pairs summing to target in BST
Given a BST with unique values, how many unique pairs of nodes sum to 10? The BST nodes are: 1, 2, 3, 7, 8, 9.
Attempts:
2 left
💡 Hint
Find all pairs (a,b) where a + b = 10 and a != b.
✗ Incorrect
Pairs are (1,9), (2,8), (3,7). Total 3 pairs.
