Challenge - 5 Problems
Two Sum in BST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Two Sum in BST with target 9
What is the output of the following code that checks if there exist two nodes in the BST whose values sum to 9?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function findTarget(root, k) { const set = new Set(); function dfs(node) { if (!node) return false; if (set.has(k - node.val)) return true; set.add(node.val); return dfs(node.left) || dfs(node.right); } return dfs(root); } const root = new TreeNode(5, new TreeNode(3, new TreeNode(2), new TreeNode(4) ), new TreeNode(6, null, new TreeNode(7) ) ); console.log(findTarget(root, 9));
Attempts:
2 left
💡 Hint
Think about whether any two nodes add up to 9 in the BST.
✗ Incorrect
The nodes with values 2 and 7 sum to 9, so the function returns true.
❓ Predict Output
intermediate2:00remaining
Output of Two Sum in BST with target 28
What is the output of the following code that checks if there exist two nodes in the BST whose values sum to 28?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function findTarget(root, k) { const set = new Set(); function dfs(node) { if (!node) return false; if (set.has(k - node.val)) return true; set.add(node.val); return dfs(node.left) || dfs(node.right); } return dfs(root); } const root = new TreeNode(10, new TreeNode(5, new TreeNode(3), new TreeNode(7) ), new TreeNode(15, null, new TreeNode(18) ) ); console.log(findTarget(root, 28));
Attempts:
2 left
💡 Hint
Check if any two nodes add up to 28 in the BST.
✗ Incorrect
The nodes with values 10 and 18 sum to 28, so the function returns true.
🔧 Debug
advanced2:00remaining
Identify the error in Two Sum in BST implementation
What error will this code produce when run?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function findTarget(root, k) { const set = new Set(); function dfs(node) { if (!node) return false; if (set.has(k - node.val)) return true; set.add(node.val); return dfs(node.left) || dfs(node.right); } return dfs(root); } const root = new TreeNode(1, new TreeNode(0), new TreeNode(3) ); console.log(findTarget(root, "3"));
Attempts:
2 left
💡 Hint
Consider JavaScript type coercion in arithmetic operations.
✗ Incorrect
No error is produced. JavaScript coerces the string "3" to number 3 during subtraction, and finds nodes 0 and 3 sum to 3, outputting true.
🧠 Conceptual
advanced1:30remaining
Why use a Set in Two Sum in BST?
Why does the Two Sum in BST algorithm use a Set to store node values during traversal?
Attempts:
2 left
💡 Hint
Think about how to quickly check if a complement value exists.
✗ Incorrect
The Set allows O(1) lookup to check if the complement (target - current node value) exists among visited nodes.
🚀 Application
expert2:30remaining
Number of pairs summing to target in BST
Given the BST below, how many unique pairs of nodes sum to 10?
BST structure:
5
/ \
3 7
/ \ \
2 4 8
Attempts:
2 left
💡 Hint
List all pairs and count those summing to 10.
✗ Incorrect
Pairs (2,8) and (3,7) sum to 10. The pair (4,6) is invalid as 6 is not in the tree. Since 5 appears once, (5,5) is not valid. So total 2 pairs.