Challenge - 5 Problems
BST Maximum Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
The maximum element in a BST is the rightmost node.
✗ Incorrect
The function moves to the right child repeatedly until it reaches the rightmost node, which holds the maximum value 20.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Check the base case for empty tree in the function.
✗ Incorrect
The function returns -1 immediately if the root is nullptr, so output is -1.
🔧 Debug
advanced2: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;
}Attempts:
2 left
💡 Hint
Check the direction of traversal inside the loop.
✗ Incorrect
The code incorrectly moves left child inside the loop that checks for right child, so it returns minimum instead of maximum.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Duplicates go to the right subtree, so maximum is the rightmost node.
✗ Incorrect
Since duplicates are inserted to the right, the maximum value is at the rightmost node including duplicates.
🚀 Application
expert2: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; }
Attempts:
2 left
💡 Hint
The maximum is the rightmost node after insertion.
✗ Incorrect
After inserting 25 as right child of 20, the rightmost node is 25, so findMax returns 25.