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
What is the output of the following code when finding the 3rd smallest element in the BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def kth_smallest(root, k): stack = [] while True: while root: stack.append(root) root = root.left root = stack.pop() k -= 1 if k == 0: return root.val root = root.right # Construct BST root = Node(5) root.left = Node(3) root.right = Node(7) root.left.left = Node(2) root.left.right = Node(4) root.right.left = Node(6) root.right.right = Node(8) print(kth_smallest(root, 3))
Attempts:
2 left
💡 Hint
Remember that inorder traversal of BST gives sorted order.
✗ Incorrect
The inorder traversal of the BST visits nodes in ascending order: 2, 3, 4, 5, 6, 7, 8.
The 3rd smallest element is 4.
🧠 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 visits the left subtree, then the node, then the right subtree.
In a BST, this order corresponds to ascending order of values.
❓ Predict Output
advancedOutput of Recursive Kth Smallest Function
What is the output of this recursive function to find the 4th smallest element in the BST below?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None count = 0 def kth_smallest_recursive(root, k): global count if not root: return None left = kth_smallest_recursive(root.left, k) if left is not None: return left count += 1 if count == k: return root.val return kth_smallest_recursive(root.right, k) # Construct BST root = Node(10) root.left = Node(5) root.right = Node(15) root.left.left = Node(3) root.left.right = Node(7) root.right.right = Node(18) print(kth_smallest_recursive(root, 4))
Attempts:
2 left
💡 Hint
Trace the inorder traversal and count nodes visited.
✗ Incorrect
The inorder traversal order is 3, 5, 7, 10, 15, 18.
The 4th smallest element is 10.
🔧 Debug
advancedIdentify the Bug in Kth Smallest Element Code
What error will this code produce when trying to find the 2nd smallest element?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def kth_smallest_buggy(root, k): stack = [] while True: while root: stack.append(root) root = root.left root = stack.pop() k -= 1 if k == 0: return root.val root = root.right # Construct BST root = Node(1) print(kth_smallest_buggy(root, 2))
Attempts:
2 left
💡 Hint
Check what happens when the stack is empty and root is None.
✗ Incorrect
The code pops from stack without checking if stack is empty after traversal.
For this tree, after visiting node 1, root and stack become empty, but loop tries to pop again causing IndexError.
🚀 Application
expertNumber of Nodes Visited to Find Kth Smallest
In a BST with 7 nodes, how many nodes does the iterative kth smallest function visit (pop from stack) to find the 5th smallest element?
Attempts:
2 left
💡 Hint
Think about inorder traversal and how many nodes are popped before reaching the kth.
✗ Incorrect
To find the 5th smallest, the function visits nodes in ascending order.
It pops nodes 1, 2, 3, 4, and 5 in order.
