Challenge - 5 Problems
BST Inorder Predecessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Inorder Predecessor Function Call
What is the output of the following C code that finds the inorder predecessor of a node with value 15 in the given BST?
DSA C
/* BST Node structure */ #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* left; struct Node* right; } Node; Node* newNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->left = node->right = NULL; return node; } Node* insert(Node* root, int data) { if (root == NULL) return newNode(data); if (data < root->data) root->left = insert(root->left, data); else root->right = insert(root->right, data); return root; } Node* inorderPredecessor(Node* root, Node* node) { Node* predecessor = NULL; while (root != NULL) { if (node->data <= root->data) { root = root->left; } else { predecessor = root; root = root->right; } } return predecessor; } int main() { Node* root = NULL; int values[] = {20, 10, 30, 5, 15, 25, 35}; for (int i = 0; i < 7; i++) { root = insert(root, values[i]); } Node node = {15, NULL, NULL}; Node* pred = inorderPredecessor(root, &node); if (pred) printf("%d\n", pred->data); else printf("No predecessor\n"); return 0; }
Attempts:
2 left
💡 Hint
Think about the largest value smaller than 15 in the BST.
✗ Incorrect
The inorder predecessor of 15 is the largest node smaller than 15. In this BST, 10 is the largest value less than 15.
🧠 Conceptual
intermediateUnderstanding Inorder Predecessor in BST
In a Binary Search Tree (BST), what does the inorder predecessor of a node represent?
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes in ascending order.
✗ Incorrect
Inorder predecessor is the node visited just before the given node in an inorder traversal, which means it has the largest value smaller than the given node.
🔧 Debug
advancedIdentify the Bug in Inorder Predecessor Function
What error will occur when running this code snippet to find the inorder predecessor of a node in a BST?
DSA C
Node* inorderPredecessor(Node* root, Node* node) {
if (node->left != NULL) {
Node* temp = node->left;
while (temp->right != NULL) {
temp = temp->right;
}
return temp;
}
Node* predecessor = NULL;
while (root != NULL) {
if (node->data < root->data) {
root = root->left;
} else if (node->data > root->data) {
predecessor = root;
root = root->right;
} else {
break;
}
}
return predecessor;
}
int main() {
Node* root = NULL;
int values[] = {20, 10, 30, 5, 15, 25, 35};
for (int i = 0; i < 7; i++) {
root = insert(root, values[i]);
}
Node node = {40, NULL, NULL};
Node* pred = inorderPredecessor(root, &node);
if (pred) printf("%d\n", pred->data);
else printf("No predecessor\n");
return 0;
}Attempts:
2 left
💡 Hint
Check what happens when the node is not present in the BST.
✗ Incorrect
The function assumes the node exists in the BST. Passing a node with value 40 (not in BST) causes the loop to break without finding the node, and later code may access NULL pointers.
📝 Syntax
advancedIdentify Syntax Error in Inorder Predecessor Code
Which option contains a syntax error in the implementation of the inorder predecessor function?
DSA C
Node* inorderPredecessor(Node* root, Node* node) {
Node* predecessor = NULL;
while (root != NULL) {
if (node->data <= root->data) {
root = root->left;
} else {
predecessor = root;
root = root->right;
}
}
return predecessor;
}Attempts:
2 left
💡 Hint
Check the syntax of the if statement condition.
✗ Incorrect
Option A is missing parentheses around the if condition, causing a syntax error in C.
🚀 Application
expertNumber of Nodes in Resulting BST After Inorder Predecessor Removal
Given a BST with nodes {20, 10, 30, 5, 15, 25, 35}, if you remove the inorder predecessor of node 20, how many nodes remain in the BST?
Attempts:
2 left
💡 Hint
Find the inorder predecessor of 20, then remove it and count remaining nodes.
✗ Incorrect
The inorder predecessor of 20 is 15. Removing 15 from the BST leaves 6 nodes.
