Challenge - 5 Problems
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind minimum element output in BST
What is the output of the following C code that finds the minimum element in a BST?
DSA C
#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 = NULL; 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; } int findMin(Node* root) { Node* current = root; while (current && current->left != NULL) { current = current->left; } return current->data; } int main() { Node* root = NULL; root = insert(root, 20); root = insert(root, 10); root = insert(root, 30); root = insert(root, 5); root = insert(root, 15); printf("%d\n", findMin(root)); return 0; }
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
✗ Incorrect
The function findMin traverses the left children until it reaches the leftmost node, which holds the minimum value. Here, 5 is the smallest inserted value.
❓ Predict Output
intermediateFind minimum element with single node BST
What will be printed when finding the minimum element in a BST with only one node?
DSA C
#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 = NULL; node->right = NULL; return node; } int findMin(Node* root) { Node* current = root; while (current && current->left != NULL) { current = current->left; } return current->data; } int main() { Node* root = newNode(42); printf("%d\n", findMin(root)); return 0; }
Attempts:
2 left
💡 Hint
With only one node, that node is the minimum.
✗ Incorrect
Since the BST has only one node with value 42, the minimum is 42 itself.
❓ Predict Output
advancedFind minimum element after inserting multiple nodes
What is the output of the following code after inserting nodes in this order: 50, 30, 70, 20, 40, 60, 80?
DSA C
#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 = NULL; 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; } int findMin(Node* root) { Node* current = root; while (current && current->left != NULL) { current = current->left; } return current->data; } int main() { Node* root = NULL; int values[] = {50, 30, 70, 20, 40, 60, 80}; for (int i = 0; i < 7; i++) { root = insert(root, values[i]); } printf("%d\n", findMin(root)); return 0; }
Attempts:
2 left
💡 Hint
The minimum is the smallest value inserted, which is the leftmost node.
✗ Incorrect
The smallest value inserted is 20, which is the leftmost node in the BST.
❓ Predict Output
advancedFind minimum element in an empty BST
What happens when you call findMin on an empty BST (root is NULL)?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* left; struct Node* right; } Node; int findMin(Node* root) { Node* current = root; while (current && current->left != NULL) { current = current->left; } return current->data; } int main() { Node* root = NULL; printf("%d\n", findMin(root)); return 0; }
Attempts:
2 left
💡 Hint
Accessing data from a NULL pointer causes a runtime error.
✗ Incorrect
Since root is NULL, current is NULL, and accessing current->data causes a segmentation fault.
🧠 Conceptual
expertWhy does findMin traverse left children in BST?
In a Binary Search Tree (BST), why does the findMin function traverse only the left children to find the minimum element?
Attempts:
2 left
💡 Hint
Think about the BST property for left and right children values.
✗ Incorrect
In a BST, left children nodes contain values smaller than their parent nodes. Therefore, the minimum value is found by going as far left as possible.
