Challenge - 5 Problems
BST Iterator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BST Iterator Next Calls
Given the BST Iterator code below, what is the output sequence of the
Next() calls?DSA C#
public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public class BSTIterator { private Stack<TreeNode> stack = new Stack<TreeNode>(); public BSTIterator(TreeNode root) { PushLeft(root); } private void PushLeft(TreeNode node) { while (node != null) { stack.Push(node); node = node.left; } } public int Next() { TreeNode node = stack.Pop(); PushLeft(node.right); return node.val; } public bool HasNext() { return stack.Count > 0; } } // BST structure: // 7 // / \ // 3 15 // / \ // 9 20 TreeNode root = new TreeNode(7, new TreeNode(3), new TreeNode(15, new TreeNode(9), new TreeNode(20)) ); BSTIterator iterator = new BSTIterator(root); List<int> result = new List<int>(); while (iterator.HasNext()) { result.Add(iterator.Next()); } Console.WriteLine(string.Join(", ", result));
Attempts:
2 left
💡 Hint
Think about how in-order traversal visits nodes in ascending order.
✗ Incorrect
The BST Iterator uses an in-order traversal approach. It pushes all left children first, so the smallest element (3) is returned first, then 7, then 9, 15, and finally 20.
🧠 Conceptual
intermediateBST Iterator Space Complexity
What is the worst-case space complexity of the BST Iterator implemented with a stack for a BST with n nodes?
Attempts:
2 left
💡 Hint
Consider the shape of the tree and how many nodes can be on the stack at once.
✗ Incorrect
In the worst case, the BST is skewed (like a linked list), so all nodes are pushed onto the stack, leading to O(n) space complexity.
🔧 Debug
advancedIdentify the Bug in BST Iterator Next Method
What error will occur when calling
Next() on this BST Iterator implementation if the stack is empty?DSA C#
public int Next() { TreeNode node = stack.Pop(); PushLeft(node.right); return node.val; }
Attempts:
2 left
💡 Hint
What happens if you pop from an empty stack in C#?
✗ Incorrect
Calling Pop() on an empty stack throws InvalidOperationException in C#.
🚀 Application
advancedUsing BST Iterator to Find kth Smallest Element
How can you use the BST Iterator to find the kth smallest element in a BST?
Attempts:
2 left
💡 Hint
The iterator returns nodes in ascending order.
✗ Incorrect
Calling Next() k times returns nodes in ascending order, so the kth call returns the kth smallest element.
🧠 Conceptual
expertBST Iterator Behavior with Concurrent Modifications
What is the expected behavior if the BST is modified (nodes added or removed) after the BST Iterator is created and before all elements are iterated?
Attempts:
2 left
💡 Hint
Think about how the stack stores nodes and if it knows about tree changes.
✗ Incorrect
The iterator uses a stack of nodes from the original tree structure. If the tree changes, the iterator's stack may become invalid, causing wrong results or exceptions.
