Challenge - 5 Problems
Two Sum 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
Given the BST below and target = 9, what is the output of the Two Sum function that returns true if two nodes sum to the target?
DSA Go
type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func findTarget(root *TreeNode, k int) bool { seen := make(map[int]bool) var dfs func(node *TreeNode) bool dfs = func(node *TreeNode) bool { if node == nil { return false } if seen[k-node.Val] { return true } seen[node.Val] = true return dfs(node.Left) || dfs(node.Right) } return dfs(root) } // BST structure: // 5 // / \ // 3 6 // / \ \ // 2 4 7 root := &TreeNode{5, &TreeNode{3, &TreeNode{2, nil, nil}, &TreeNode{4, nil, nil}}, &TreeNode{6, nil, &TreeNode{7, nil, nil}}} result := findTarget(root, 9) fmt.Println(result)
Attempts:
2 left
💡 Hint
Think about pairs of nodes that add up to 9 in the BST.
✗ Incorrect
The function uses a map to track seen values. It finds 2 and 7 which sum to 9, so returns true.
🧠 Conceptual
intermediate1:00remaining
What data structure helps find two sum in BST efficiently?
Which data structure is most useful to check if the complement of a node's value exists while traversing a BST for two sum?
Attempts:
2 left
💡 Hint
Think about fast lookup of complements.
✗ Incorrect
A hash map allows O(1) average time lookup to check if complement exists.
🔧 Debug
advanced2:00remaining
Identify the error in Two Sum BST code snippet
What error does this Go code produce when run?
func findTarget(root *TreeNode, k int) bool {
seen := make(map[int]bool)
var dfs func(node *TreeNode) bool
dfs = func(node *TreeNode) bool {
if node == nil {
return false
}
if seen[k-node.Val] {
return true
}
seen[node.Val] = true
return dfs(node.Left) && dfs(node.Right)
}
return dfs(root)
}
Attempts:
2 left
💡 Hint
Check the boolean operator used in recursion return.
✗ Incorrect
Using && requires both left and right to be true, so it misses pairs found in only one subtree.
🚀 Application
advanced1:30remaining
How many pairs sum to 10 in this BST?
Given the BST below, how many unique pairs of nodes sum to 10?
// BST:
// 8
// / \
// 3 10
// / \ \
// 1 6 14
// / \ /
// 4 7 13
Attempts:
2 left
💡 Hint
Check pairs like (3,7), (6,4), (1,9) but 9 is not in tree.
✗ Incorrect
Pairs summing to 10 are (3,7), (6,4), and (10,0) but 0 not in tree, so only 2 pairs. Actually (8,2) no 2 in tree. Also (14,-4) no negative. So only (3,7) and (6,4). Wait, 13+? No. So 2 pairs.
📝 Syntax
expert3:00remaining
Which option correctly implements Two Sum in BST using inorder traversal?
Select the correct Go code snippet that uses inorder traversal and two pointers to find if two nodes sum to target k in a BST.
Attempts:
2 left
💡 Hint
Inorder traversal gives sorted array; use two pointers moving inward.
✗ Incorrect
Option B correctly does inorder traversal (left-root-right) to get sorted values, then uses two pointers moving inward to find sum k.