Challenge - 5 Problems
BST Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Inorder Traversal on a BST
What is the output of the inorder traversal of this BST after inserting the values 10, 5, 15, 3, 7 in that order?
DSA C
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Assume insert function inserts nodes maintaining BST property
// Inorder traversal prints nodes in sorted order
// Insert sequence: 10, 5, 15, 3, 7
// Then perform inorder traversal and print values separated by spaces.Attempts:
2 left
💡 Hint
Inorder traversal of a BST prints values in ascending order.
✗ Incorrect
Inorder traversal visits left subtree, then node, then right subtree. For BST, this results in sorted order of values.
🧠 Conceptual
intermediateWhy BST Property is Important for Search Efficiency
Why does the BST property help in efficient searching compared to an unsorted binary tree?
Attempts:
2 left
💡 Hint
Think about how the BST property orders values relative to each node.
✗ Incorrect
The BST property ensures left subtree has smaller values and right subtree has larger values, allowing search to discard half the tree at each step.
🔧 Debug
advancedIdentify the Error in BST Insertion Code
What error will this BST insertion code cause when inserting a value equal to a node's data?
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 if (val > root->data) { root->right = insert(root->right, val); } // No else case for val == root->data return root; }
Attempts:
2 left
💡 Hint
Check what happens when val equals root->data.
✗ Incorrect
Since no else case handles val == root->data, the function returns root without inserting duplicates, effectively ignoring them.
❓ Predict Output
advancedOutput After Deleting a Node with Two Children in BST
Given a BST with nodes 20, 10, 30, 5, 15, 25, 35, what is the inorder traversal output after deleting node 20?
DSA C
// BST nodes inserted in order: 20,10,30,5,15,25,35 // Delete node with value 20 (which has two children) // Then perform inorder traversal and print values separated by spaces.
Attempts:
2 left
💡 Hint
When deleting a node with two children, replace it with its inorder successor.
✗ Incorrect
Node 20 is replaced by its inorder successor 25, then 25 is deleted from right subtree. Inorder traversal reflects updated tree.
🧠 Conceptual
expertEffect of Violating BST Property on Search
If a binary tree does not maintain the BST property, what is the worst-case time complexity of searching for a value compared to a BST?
Attempts:
2 left
💡 Hint
Without BST property, you cannot skip half the nodes during search.
✗ Incorrect
Without BST property, search may need to check every node, resulting in linear time O(n).
