0
0
DSA C++programming~20 mins

BST Search Operation in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A7
BNot found
C10
D15
Attempts:
2 left
💡 Hint
Think about how the search function returns the node if found.
Predict Output
intermediate
2: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;
}
A7
BNot found
C8
D3
Attempts:
2 left
💡 Hint
Check if the value 8 exists in the BST nodes.
🧠 Conceptual
advanced
1: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?
AO(n)
BO(log n)
CO(n log n)
DO(1)
Attempts:
2 left
💡 Hint
Consider the shape of the BST in the worst case.
🔧 Debug
advanced
2: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);
}
ASegmentation fault (accessing null pointer)
BReturns nullptr correctly
CCompilation error due to missing return
DInfinite recursion
Attempts:
2 left
💡 Hint
What happens if root is nullptr?
🚀 Application
expert
2: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?
A4
B2
C3
D5
Attempts:
2 left
💡 Hint
Count nodes visited from root to the target node.