Challenge - 5 Problems
BST Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of BST Search for Existing Value
What is the output of the following C++ code that searches for the value 15 in a BST?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
Node* insert(Node* root, int val) {
if (!root) return new Node(val);
if (val < root->data) root->left = insert(root->left, val);
else root->right = insert(root->right, val);
return root;
}
Node* search(Node* root, int val) {
if (!root || root->data == val) return root;
if (val < root->data) return search(root->left, val);
return search(root->right, val);
}
int main() {
Node* root = nullptr;
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 3);
root = insert(root, 7);
Node* result = search(root, 15);
if (result) std::cout << result->data << std::endl;
else std::cout << "Not found" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Think about how the search function returns the node if found.
✗ Incorrect
The search function returns the node with data 15 because it exists in the BST. The output prints the data of the found node.
❓ Predict Output
intermediate2:00remaining
Output of BST Search for Non-Existing Value
What is the output of the following C++ code that searches for the value 8 in a BST?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
Node* insert(Node* root, int val) {
if (!root) return new Node(val);
if (val < root->data) root->left = insert(root->left, val);
else root->right = insert(root->right, val);
return root;
}
Node* search(Node* root, int val) {
if (!root || root->data == val) return root;
if (val < root->data) return search(root->left, val);
return search(root->right, val);
}
int main() {
Node* root = nullptr;
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 3);
root = insert(root, 7);
Node* result = search(root, 8);
if (result) std::cout << result->data << std::endl;
else std::cout << "Not found" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Check if the value 8 exists in the BST nodes.
✗ Incorrect
The value 8 is not inserted in the BST, so the search returns nullptr and prints 'Not found'.
🧠 Conceptual
advanced1:30remaining
Time Complexity of BST Search
What is the worst-case time complexity of searching for a value in a Binary Search Tree (BST) with n nodes?
Attempts:
2 left
💡 Hint
Consider the shape of the BST in the worst case.
✗ Incorrect
In the worst case, the BST is skewed (like a linked list), so search takes O(n) time.
🔧 Debug
advanced2:00remaining
Identify the Bug in BST Search Function
What error will occur when running this BST search function if the value is not found?
DSA C++
Node* search(Node* root, int val) { if (root == nullptr) return nullptr; if (root->data == val) return root; if (val < root->data) return search(root->left, val); return search(root->right, val); }
Attempts:
2 left
💡 Hint
What happens if root is nullptr?
✗ Incorrect
The function does not check if root is nullptr before accessing root->data, causing a segmentation fault if value is not found.
🚀 Application
expert2:30remaining
Number of Nodes Visited During BST Search
Given a BST with nodes inserted in this order: 20, 10, 30, 5, 15, 25, 35. How many nodes are visited when searching for the value 25?
Attempts:
2 left
💡 Hint
Count nodes visited from root to the target node.
✗ Incorrect
Search path: 20 -> 30 -> 25. Three nodes visited.