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
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 C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool findTarget(TreeNode* root, int k) {
std::unordered_set<int> set;
std::function<bool(TreeNode*)> dfs = [&](TreeNode* node) {
if (!node) return false;
if (set.count(k - node->val)) return true;
set.insert(node->val);
return dfs(node->left) || dfs(node->right);
};
return dfs(root);
}
// BST:
// 5
// / \
// 3 6
// / \ \
// 2 4 7
// Call: findTarget(root, 9);Attempts:
2 left
💡 Hint
Think about pairs of nodes that 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
Given the BST below and target = 28, what is the output of the Two Sum function that returns true if two nodes sum to the target?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
bool findTarget(TreeNode* root, int k) {
std::unordered_set<int> set;
std::function<bool(TreeNode*)> dfs = [&](TreeNode* node) {
if (!node) return false;
if (set.count(k - node->val)) return true;
set.insert(node->val);
return dfs(node->left) || dfs(node->right);
};
return dfs(root);
}
// BST:
// 10
// / \
// 5 15
// / \ \
// 3 7 18
// Call: findTarget(root, 28);Attempts:
2 left
💡 Hint
Check if any two nodes sum to 28.
✗ 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 BST code
What error does the following Two Sum in BST code produce when run?
DSA C++
bool findTarget(TreeNode* root, int k) { std::unordered_set<int> set; std::function<bool(TreeNode*)> dfs = [&](TreeNode* node) { if (!node) return false; if (set.count(k - node->val)) return true; set.insert(node->val); return dfs(node->left) && dfs(node->right); }; return dfs(root); }
Attempts:
2 left
💡 Hint
Look at the logical operator used in recursive calls.
✗ Incorrect
Using && means both left and right must be true to continue, so it stops early and returns false incorrectly.
🧠 Conceptual
advanced2:00remaining
Time complexity of Two Sum in BST using hash set
What is the time complexity of finding if there exist two elements in a BST that sum to a target using a hash set and DFS traversal?
Attempts:
2 left
💡 Hint
Consider visiting each node once and hash set operations.
✗ Incorrect
Each node is visited once, and hash set lookups and inserts are O(1) on average, so total is O(n).
🚀 Application
expert3:00remaining
Output of Two Sum in BST using two-pointer approach
Given the BST below and target = 16, what is the output of the Two Sum function implemented using two iterators (inorder and reverse inorder) to find if two nodes sum to the target?
DSA C++
#include <stack> struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; class BSTIterator { std::stack<TreeNode*> stk; bool reverse; public: BSTIterator(TreeNode* root, bool rev) : reverse(rev) { pushAll(root); } void pushAll(TreeNode* node) { while (node) { stk.push(node); node = reverse ? node->right : node->left; } } int next() { TreeNode* tmp = stk.top(); stk.pop(); if (!reverse) pushAll(tmp->right); else pushAll(tmp->left); return tmp->val; } bool hasNext() { return !stk.empty(); } }; bool findTarget(TreeNode* root, int k) { if (!root) return false; BSTIterator l(root, false); BSTIterator r(root, true); int i = l.next(); int j = r.next(); while (i < j) { if (i + j == k) return true; else if (i + j < k) i = l.next(); else j = r.next(); } return false; } // BST: // 8 // / \ // 3 10 // / \ \ // 1 6 14 // / \ / // 4 7 13 // Call: findTarget(root, 16);
Attempts:
2 left
💡 Hint
Check pairs from smallest and largest values moving inward.
✗ Incorrect
Nodes 3 and 13 sum to 16, so the function returns true.