Challenge - 5 Problems
BST Maximum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the maximum element in a BST
What is the output of the following C code that finds the maximum element in a Binary Search Tree (BST)?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int findMax(Node* root) {
if (root == NULL) return -1;
while (root->right != NULL) {
root = root->right;
}
return root->data;
}
int main() {
Node n1 = {10, NULL, NULL};
Node n2 = {20, NULL, NULL};
Node n3 = {5, NULL, NULL};
Node n4 = {15, NULL, NULL};
n1.right = &n2;
n1.left = &n3;
n2.left = &n4;
int max = findMax(&n1);
printf("%d\n", max);
return 0;
}Attempts:
2 left
💡 Hint
The maximum element in a BST is the rightmost node.
✗ Incorrect
In a BST, the maximum value is found by going as far right as possible. Here, node with value 20 is the rightmost node.
❓ Predict Output
intermediateOutput when BST is empty
What will be the output of the following code when the BST is empty (root is NULL)?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int findMax(Node* root) {
if (root == NULL) return -1;
while (root->right != NULL) {
root = root->right;
}
return root->data;
}
int main() {
Node* root = NULL;
int max = findMax(root);
printf("%d\n", max);
return 0;
}Attempts:
2 left
💡 Hint
Check the base case when root is NULL.
✗ Incorrect
The function returns -1 explicitly when the root is NULL, so the output is -1.
🔧 Debug
advancedIdentify the error in BST maximum element function
What error will the following code produce when trying to find the maximum element in a BST?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int findMax(Node* root) {
while (root->right != NULL) {
root = root->right;
}
return root->data;
}
int main() {
Node* root = NULL;
int max = findMax(root);
printf("%d\n", max);
return 0;
}Attempts:
2 left
💡 Hint
What happens if root is NULL and you access root->right?
✗ Incorrect
The function does not check if root is NULL before accessing root->right, causing a segmentation fault.
🧠 Conceptual
advancedWhy does finding maximum in BST take O(h) time?
Why does the function to find the maximum element in a BST take time proportional to the height (h) of the tree?
Attempts:
2 left
💡 Hint
Think about the path taken to find the maximum in a BST.
✗ Incorrect
The maximum element is found by moving right from the root until no more right child exists, which is at most the height of the tree.
❓ Predict Output
expertOutput of modified BST maximum function with recursion
What is the output of the following recursive function to find the maximum element in a BST?
DSA C
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int findMax(Node* root) {
if (root == NULL) return -1;
if (root->right == NULL) return root->data;
return findMax(root->right);
}
int main() {
Node n1 = {8, NULL, NULL};
Node n2 = {12, NULL, NULL};
Node n3 = {3, NULL, NULL};
Node n4 = {10, NULL, NULL};
n1.left = &n3;
n1.right = &n2;
n2.left = &n4;
int max = findMax(&n1);
printf("%d\n", max);
return 0;
}Attempts:
2 left
💡 Hint
The maximum is the rightmost node's data.
✗ Incorrect
The function recursively goes right until it finds the node with no right child, which is 12 here.
