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 Kth Smallest Element Function Call
What is the output of the following JavaScript code that finds the 3rd smallest element in a BST?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function kthSmallest(root, k) { let count = 0; let result = null; function inorder(node) { if (!node || result !== null) return; inorder(node.left); count++; if (count === k) { result = node.val; return; } inorder(node.right); } inorder(root); return result; } const root = new TreeNode(5, new TreeNode(3, new TreeNode(2, new TreeNode(1), null), new TreeNode(4)), new TreeNode(6) ); console.log(kthSmallest(root, 3));
Attempts:
2 left
💡 Hint
Remember that inorder traversal of BST gives sorted order.
✗ Incorrect
The inorder traversal visits nodes in ascending order: 1, 2, 3, 4, 5, 6. The 3rd smallest is 3.
🧠 Conceptual
intermediate1:30remaining
Understanding Inorder Traversal in BST
Why does inorder traversal help find the kth smallest element in a BST?
Attempts:
2 left
💡 Hint
Think about the BST property and how inorder traversal works.
✗ Incorrect
In BST, left child < parent < right child. Inorder traversal visits left subtree, then node, then right subtree, producing sorted order.
🔧 Debug
advanced2:00remaining
Identify the Error in Kth Smallest Implementation
What error will occur when running this code to find the kth smallest element?
DSA Javascript
function kthSmallest(root, k) {
let count = 0;
let result = null;
function inorder(node) {
if (!node) return;
inorder(node.left);
if (count === k) {
result = node.val;
return;
}
count++;
inorder(node.right);
}
inorder(root);
return result;
}Attempts:
2 left
💡 Hint
Check the order of count increment and comparison with k.
✗ Incorrect
The count is checked before incrementing, so count never reaches k at the right time, causing result to remain null.
❓ Predict Output
advanced2:00remaining
Output of Kth Smallest with Duplicate Values
What is the output of the code below when finding the 4th smallest element in a BST with duplicates?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function kthSmallest(root, k) { let count = 0; let result = null; function inorder(node) { if (!node || result !== null) return; inorder(node.left); count++; if (count === k) { result = node.val; return; } inorder(node.right); } inorder(root); return result; } const root = new TreeNode(5, new TreeNode(3, new TreeNode(3, new TreeNode(2), null), new TreeNode(4)), new TreeNode(6) ); console.log(kthSmallest(root, 4));
Attempts:
2 left
💡 Hint
Duplicates appear in sorted order during inorder traversal.
✗ Incorrect
Inorder traversal order: 2, 3, 3, 4, 5, 6. The 4th smallest is 4.
🚀 Application
expert2:30remaining
Optimizing Kth Smallest Element Search
Which data structure modification allows finding the kth smallest element in a BST in O(log n) time on average?
Attempts:
2 left
💡 Hint
Think about how to quickly skip nodes during traversal.
✗ Incorrect
By storing the size of left subtree at each node, we can decide whether to go left, right, or return current node quickly.