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 Check in BST
What is the output of the following code that checks if there exist two nodes in the BST whose values sum to 9?
DSA C
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
} Node;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool findTargetHelper(Node* root, int k, int* arr, int* size) {
if (!root) return false;
if (findTargetHelper(root->left, k, arr, size)) return true;
for (int i = 0; i < *size; i++) {
if (arr[i] == k - root->val) return true;
}
arr[(*size)++] = root->val;
return findTargetHelper(root->right, k, arr, size);
}
bool findTarget(Node* root, int k) {
int arr[1000];
int size = 0;
return findTargetHelper(root, k, arr, &size);
}
int main() {
Node n1 = {5, NULL, NULL};
Node n2 = {3, NULL, NULL};
Node n3 = {6, NULL, NULL};
Node n4 = {2, NULL, NULL};
Node n5 = {4, NULL, NULL};
Node n6 = {7, NULL, NULL};
n1.left = &n2; n1.right = &n3;
n2.left = &n4; n2.right = &n5;
n3.right = &n6;
if (findTarget(&n1, 9)) {
printf("true\n");
} else {
printf("false\n");
}
return 0;
}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
intermediateNumber of Unique Pairs Summing to Target in BST
Given a BST with unique values, how many unique pairs of nodes sum to 10 in this BST?
BST nodes: 1, 3, 4, 5, 7, 9
Attempts:
2 left
💡 Hint
Check pairs that add up to 10 without repeating nodes.
✗ Incorrect
Pairs are (1,9), (3,7), and (4,6) but 6 is not in BST, so only (1,9) and (3,7) count. Also (5,5) is invalid since nodes are unique. So total 2 pairs.
❓ Predict Output
advancedOutput of Two Sum Using Inorder and Two Pointers
What is the output of the following code that uses inorder traversal and two pointers to find if two nodes sum to 11?
DSA C
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct Node { int val; struct Node* left; struct Node* right; } Node; void inorder(Node* root, int* arr, int* size) { if (!root) return; inorder(root->left, arr, size); arr[(*size)++] = root->val; inorder(root->right, arr, size); } bool findTarget(Node* root, int k) { int arr[1000]; int size = 0; inorder(root, arr, &size); int left = 0, right = size - 1; while (left < right) { int sum = arr[left] + arr[right]; if (sum == k) return true; else if (sum < k) left++; else right--; } return false; } int main() { Node n1 = {8, NULL, NULL}; Node n2 = {3, NULL, NULL}; Node n3 = {10, NULL, NULL}; Node n4 = {1, NULL, NULL}; Node n5 = {6, NULL, NULL}; Node n6 = {14, NULL, NULL}; Node n7 = {4, NULL, NULL}; Node n8 = {7, NULL, NULL}; n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; n3.right = &n6; n5.left = &n7; n5.right = &n8; if (findTarget(&n1, 11)) { printf("true\n"); } else { printf("false\n"); } return 0; }
Attempts:
2 left
💡 Hint
Use the sorted array from inorder traversal to find pairs.
✗ Incorrect
Inorder traversal gives sorted array [1,3,4,6,7,8,10,14]. The pair (4,7) sums to 11, so output is true.
🔧 Debug
advancedIdentify the Bug in Two Sum BST Code
What error will this code produce when checking if two nodes sum to 9 in the BST?
DSA C
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct Node { int val; struct Node* left; struct Node* right; } Node; bool findTarget(Node* root, int k) { if (!root) return false; bool left = findTarget(root->left, k); bool right = findTarget(root->right, k); if (left || right) return true; if (root->val == k - root->val) return true; return false; } int main() { Node n1 = {5, NULL, NULL}; Node n2 = {3, NULL, NULL}; Node n3 = {6, NULL, NULL}; Node n4 = {2, NULL, NULL}; Node n5 = {4, NULL, NULL}; Node n6 = {7, NULL, NULL}; n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; n3.right = &n6; if (findTarget(&n1, 9)) { printf("true\n"); } else { printf("false\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check how the code checks pairs and if it stores visited nodes.
✗ Incorrect
The code only checks if root->val equals k - root->val, which is only true if k is twice root->val. It does not check pairs of different nodes, so it always returns false for pairs.
🚀 Application
expertMinimum Space Two Sum in BST
Which approach uses the least extra space to check if two nodes in a BST sum to a target value k?
Attempts:
2 left
💡 Hint
Think about how to avoid storing all nodes or many values.
✗ Incorrect
Using two iterators (one from smallest, one from largest) simulates two pointers without storing all nodes, using O(h) space where h is tree height, less than storing all nodes.
