Challenge - 5 Problems
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Inorder Traversal for Kth Smallest
What is the output of the inorder traversal of this BST before finding the 3rd smallest element?
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
Node* root = new Node(5);
root->left = new Node(3);
root->right = new Node(7);
root->left->left = new Node(2);
root->left->right = new Node(4);
root->right->left = new Node(6);
root->right->right = new Node(8);
void inorder(Node* node, vector<int>& res) {
if (!node) return;
inorder(node->left, res);
res.push_back(node->val);
inorder(node->right, res);
}
vector<int> result;
inorder(root, result);
for (int v : result) cout << v << " ";Attempts:
2 left
💡 Hint
Inorder traversal of BST visits nodes in ascending order.
✗ Incorrect
Inorder traversal visits left subtree, then node, then right subtree. For BST, this gives sorted order.
❓ Predict Output
intermediate2:00remaining
Output of Kth Smallest Element Function
What is the output of the function that finds the 4th smallest element in the BST below?
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
int count = 0;
int kthSmallest = -1;
void inorderKth(Node* node, int k) {
if (!node || count >= k) return;
inorderKth(node->left, k);
count++;
if (count == k) {
kthSmallest = node->val;
return;
}
inorderKth(node->right, k);
}
Node* root = new Node(5);
root->left = new Node(3);
root->right = new Node(7);
root->left->left = new Node(2);
root->left->right = new Node(4);
root->right->left = new Node(6);
root->right->right = new Node(8);
inorderKth(root, 4);
cout << kthSmallest;Attempts:
2 left
💡 Hint
Count nodes visited in ascending order until reaching k.
✗ Incorrect
The 4th smallest element in sorted order [2,3,4,5,6,7,8] is 5.
🔧 Debug
advanced2:00remaining
Identify the Bug in Kth Smallest Element Code
What error will this code produce when trying to find the 2nd smallest element?
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
int count = 0;
int kthSmallest = -1;
void inorderKth(Node* node, int k) {
if (!node) return;
inorderKth(node->left, k);
count++;
if (count == k) {
kthSmallest = node->val;
}
inorderKth(node->right, k);
}
Node* root = new Node(3);
root->left = new Node(1);
root->right = new Node(4);
root->left->right = new Node(2);
inorderKth(root, 2);
cout << kthSmallest;Attempts:
2 left
💡 Hint
Check if traversal stops after finding kth element.
✗ Incorrect
The code does not stop traversal after finding kth element, so kthSmallest gets overwritten by later nodes.
🧠 Conceptual
advanced1:30remaining
Time Complexity of Kth Smallest Element in BST
What is the average time complexity to find the kth smallest element in a balanced BST using inorder traversal?
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes in ascending order until kth is found.
✗ Incorrect
We only need to visit k nodes in inorder traversal, so average time is O(k).
🚀 Application
expert2:30remaining
Result of Kth Smallest Element After Tree Modification
Given the BST below, after inserting a new node with value 1, what is the 3rd smallest element?
DSA C++
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
Node* root = new Node(5);
root->left = new Node(3);
root->right = new Node(7);
root->left->left = new Node(2);
root->left->right = new Node(4);
root->right->left = new Node(6);
root->right->right = new Node(8);
// Insert new node with value 1
root->left->left->left = new Node(1);
int count = 0;
int kthSmallest = -1;
void inorderKth(Node* node, int k) {
if (!node || count >= k) return;
inorderKth(node->left, k);
count++;
if (count == k) {
kthSmallest = node->val;
return;
}
inorderKth(node->right, k);
}
inorderKth(root, 3);
cout << kthSmallest;Attempts:
2 left
💡 Hint
Remember to include the new node in the sorted order.
✗ Incorrect
After insertion, inorder traversal is [1,2,3,4,5,6,7,8]. The 3rd smallest is 2.