0
0
DSA C++programming~20 mins

BST Find Maximum Element in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Maximum Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find maximum element in a BST
What is the output of the following C++ code that finds the maximum element in a Binary Search Tree?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
};

int findMax(Node* root) {
    if (!root) return -1;
    while (root->right) {
        root = root->right;
    }
    return root->data;
}

int main() {
    Node n1 = {10, nullptr, nullptr};
    Node n2 = {20, nullptr, nullptr};
    Node n3 = {5, nullptr, nullptr};
    n1.right = &n2;
    n1.left = &n3;
    int maxVal = findMax(&n1);
    std::cout << maxVal << std::endl;
    return 0;
}
A-1
B20
C10
D5
Attempts:
2 left
💡 Hint
The maximum element in a BST is the rightmost node.
Predict Output
intermediate
2:00remaining
Output when BST is empty
What will be the output of the following code when the BST root is nullptr?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
};

int findMax(Node* root) {
    if (!root) return -1;
    while (root->right) {
        root = root->right;
    }
    return root->data;
}

int main() {
    Node* root = nullptr;
    int maxVal = findMax(root);
    std::cout << maxVal << std::endl;
    return 0;
}
A-1
B0
CSegmentation fault
DUndefined behavior
Attempts:
2 left
💡 Hint
Check the base case for empty tree in the function.
🔧 Debug
advanced
2:00remaining
Identify the error in maximum element finder
What error will the following code produce when trying to find the maximum element in a BST?
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
};

int findMax(Node* root) {
    while (root->right != nullptr) {
        root = root->left;
    }
    return root->data;
}
AReturns minimum element instead of maximum
BSegmentation fault due to null pointer dereference
CReturns maximum element correctly
DCompilation error due to missing return statement
Attempts:
2 left
💡 Hint
Check the direction of traversal inside the loop.
🧠 Conceptual
advanced
2:00remaining
Maximum element in BST with duplicates
In a BST that allows duplicate values inserted to the right subtree, which node will the findMax function return?
AThe root node always
BThe leftmost node with the maximum value
CThe rightmost node with the maximum value including duplicates
DThe minimum value node
Attempts:
2 left
💡 Hint
Duplicates go to the right subtree, so maximum is the rightmost node.
🚀 Application
expert
2:00remaining
Find maximum element after BST modification
Given a BST where a new node with value 25 is inserted as the right child of the node with value 20, what will be the output of findMax function?
DSA C++
#include <iostream>

struct Node {
    int data;
    Node* left;
    Node* right;
};

int findMax(Node* root) {
    if (!root) return -1;
    while (root->right) {
        root = root->right;
    }
    return root->data;
}

int main() {
    Node n1 = {10, nullptr, nullptr};
    Node n2 = {20, nullptr, nullptr};
    Node n3 = {5, nullptr, nullptr};
    Node n4 = {25, nullptr, nullptr};
    n1.left = &n3;
    n1.right = &n2;
    n2.right = &n4;
    int maxVal = findMax(&n1);
    std::cout << maxVal << std::endl;
    return 0;
}
A5
B20
C10
D25
Attempts:
2 left
💡 Hint
The maximum is the rightmost node after insertion.