Challenge - 5 Problems
Kth Smallest Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kth Smallest Element Function Call
What is the output of the following Java code that finds the 3rd smallest element in the given BST?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { int count = 0; int result = -1; public int kthSmallest(TreeNode root, int k) { inorder(root, k); return result; } private void inorder(TreeNode node, int k) { if (node == null) return; inorder(node.left, k); count++; if (count == k) { result = node.val; return; } inorder(node.right, k); } public static void main(String[] args) { TreeNode root = new TreeNode(5); root.left = new TreeNode(3); root.right = new TreeNode(6); root.left.left = new TreeNode(2); root.left.right = new TreeNode(4); root.left.left.left = new TreeNode(1); Solution sol = new Solution(); System.out.println(sol.kthSmallest(root, 3)); } }
Attempts:
2 left
💡 Hint
Remember that inorder traversal of BST visits nodes in ascending order.
✗ Incorrect
The inorder traversal visits nodes in ascending order: 1, 2, 3, 4, 5, 6. The 3rd smallest element is 3.
🧠 Conceptual
intermediateUnderstanding Inorder Traversal in BST
Why does inorder traversal help in finding the kth smallest element in a BST?
Attempts:
2 left
💡 Hint
Think about the BST property and how inorder traversal works.
✗ Incorrect
Inorder traversal of a BST visits nodes from smallest to largest, so counting nodes during traversal gives the kth smallest element.
🔧 Debug
advancedAnalyze the Kth Smallest Element Code
Which of the following is true about this code for finding the kth smallest element in a BST?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { int count = 0; int result = -1; public int kthSmallest(TreeNode root, int k) { inorder(root, k); return result; } private void inorder(TreeNode node, int k) { if (node == null) return; inorder(node.left, k); if (count == k) return; count++; result = node.val; inorder(node.right, k); } }
Attempts:
2 left
💡 Hint
Observe the order: recurse left, check count, increment and set if not, recurse right. Note how pruning works.
✗ Incorrect
No error occurs. The code correctly returns the kth smallest element. It updates result for each of the first k nodes in inorder order (overwriting previous values), and the early return prunes subtrees after reaching k, preventing further updates.
❓ Predict Output
advancedOutput of Kth Smallest Element with Duplicate Values
What is the output of the following code when finding the 4th smallest element in a BST that contains duplicates?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { int count = 0; int result = -1; public int kthSmallest(TreeNode root, int k) { inorder(root, k); return result; } private void inorder(TreeNode node, int k) { if (node == null) return; inorder(node.left, k); count++; if (count == k) { result = node.val; return; } inorder(node.right, k); } public static void main(String[] args) { TreeNode root = new TreeNode(5); root.left = new TreeNode(3); root.right = new TreeNode(6); root.left.left = new TreeNode(3); root.left.right = new TreeNode(4); root.left.left.left = new TreeNode(1); Solution sol = new Solution(); System.out.println(sol.kthSmallest(root, 4)); } }
Attempts:
2 left
💡 Hint
Duplicates are counted as separate nodes in inorder traversal.
✗ Incorrect
Inorder traversal order: 1, 3, 3, 4, 5, 6. The 4th smallest element is 4.
🧠 Conceptual
expertTime Complexity of Kth Smallest Element in BST
What is the average time complexity of finding the kth smallest element in a balanced BST using inorder traversal?
Attempts:
2 left
💡 Hint
Inorder traversal stops once kth element is found.
✗ Incorrect
Inorder traversal visits nodes in ascending order and stops after kth node, so it visits only k nodes on average.
