Challenge - 5 Problems
BST Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes in BST
What is the printed state of the BST after inserting the values 10, 5, 15, 3, 7 in that order? The BST is printed using in-order traversal (left-root-right).
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* insert(struct Node* root, int val) {
if (root == NULL) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
if (val < root->data) {
root->left = insert(root->left, val);
} else {
root->right = insert(root->right, val);
}
return root;
}
void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d -> ", root->data);
inorder(root->right);
}
}
int main() {
struct Node* root = NULL;
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 15);
root = insert(root, 3);
root = insert(root, 7);
inorder(root);
printf("null\n");
return 0;
}Attempts:
2 left
💡 Hint
Remember that in-order traversal of a BST prints values in ascending order.
✗ Incorrect
In-order traversal visits nodes in left-root-right order. After inserting 10, 5, 15, 3, 7, the BST structure is:
10
/ \
5 15
/ \
3 7
So the in-order traversal prints: 3 -> 5 -> 7 -> 10 -> 15 -> null.
❓ Predict Output
intermediateBST structure after inserting duplicate value
What is the in-order traversal output after inserting the values 20, 10, 30, 20 into an empty BST? Assume duplicates are inserted to the right subtree.
DSA C
struct Node* insert(struct Node* root, int val) { if (root == NULL) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = val; newNode->left = NULL; newNode->right = NULL; return newNode; } if (val < root->data) { root->left = insert(root->left, val); } else { root->right = insert(root->right, val); } return root; } // In-order traversal same as before
Attempts:
2 left
💡 Hint
Duplicates go to the right subtree, so the second 20 is placed as right child of the first 20.
✗ Incorrect
The BST after insertions:
20
/ \
10 30
\
20
In-order traversal visits left-root-right, so output is 10 -> 20 -> 20 -> 30 -> null.
🔧 Debug
advancedIdentify the error in BST insert function
What error will occur when running this BST insert function code snippet?
DSA C
struct Node* insert(struct Node* root, int val) { if (root == NULL) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = val; newNode->left = NULL; newNode->right = NULL; return newNode; } if (val <= root->data) { root->left = insert(root->left, val); } else { root->right = insert(root->right, val); } return root; } // main calls insert with root = NULL and val = 5
Attempts:
2 left
💡 Hint
Check base case and recursive calls carefully.
✗ Incorrect
The insert function correctly handles the base case when root is NULL by allocating a new node. Recursive calls correctly update left or right child pointers. No infinite recursion or uninitialized pointers occur. So the code runs correctly.
❓ Predict Output
advancedBST structure after inserting negative and positive values
What is the in-order traversal output after inserting the values 0, -10, 10, -20, 20 into an empty BST?
DSA C
struct Node* insert(struct Node* root, int val) { if (root == NULL) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = val; newNode->left = NULL; newNode->right = NULL; return newNode; } if (val < root->data) { root->left = insert(root->left, val); } else { root->right = insert(root->right, val); } return root; } // In-order traversal same as before
Attempts:
2 left
💡 Hint
In-order traversal prints values in ascending order.
✗ Incorrect
The BST structure after insertions:
0
/ \
-10 10
/ \
-20 20
In-order traversal visits nodes in ascending order: -20 -> -10 -> 0 -> 10 -> 20 -> null.
🧠 Conceptual
expertNumber of nodes in BST after insertions
If you insert the values 8, 3, 10, 1, 6, 14, 4, 7, 13 into an empty BST, how many nodes will the BST contain?
Attempts:
2 left
💡 Hint
Each insertion adds one node since no duplicates are inserted.
✗ Incorrect
All inserted values are unique. Each insertion adds one node. Total nodes = number of inserted values = 9.
