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 initialized with the BST below, what is the output sequence of calling next() three times?
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class BSTIterator: def __init__(self, root): self.stack = [] self._push_left(root) def _push_left(self, node): while node: self.stack.append(node) node = node.left def next(self): node = self.stack.pop() self._push_left(node.right) return node.val def hasNext(self): return len(self.stack) > 0 # BST structure: # 7 # / \ # 3 15 # / \ # 9 20 root = TreeNode(7, TreeNode(3), TreeNode(15, TreeNode(9), TreeNode(20))) iterator = BSTIterator(root) output = [iterator.next(), iterator.next(), iterator.next()]
Attempts:
2 left
💡 Hint
Remember that BST Iterator returns elements in ascending order using in-order traversal.
✗ Incorrect
The BST Iterator uses a stack to simulate in-order traversal. The first next() returns the smallest element 3, then 7, then 9.
🧠 Conceptual
intermediateBST Iterator hasNext() behavior
After initializing a BST Iterator on a BST with nodes [5, 3, 7], which statement about hasNext() is true immediately after initialization?
Attempts:
2 left
💡 Hint
Think about what the stack contains right after initialization.
✗ Incorrect
The iterator pushes all left nodes from root to the stack during initialization, so stack is not empty and hasNext() returns True.
🔧 Debug
advancedIdentify the error in BST Iterator next() method
What error will occur when calling next() on this BST Iterator implementation if the stack is empty?
DSA Python
class BSTIterator: def __init__(self, root): self.stack = [] self._push_left(root) def _push_left(self, node): while node: self.stack.append(node) node = node.left def next(self): node = self.stack.pop() self._push_left(node.right) return node.val def hasNext(self): return len(self.stack) > 0 iterator = BSTIterator(None) iterator.next()
Attempts:
2 left
💡 Hint
What happens if you pop from an empty list in Python?
✗ Incorrect
Calling pop() on an empty list raises IndexError in Python.
🚀 Application
advancedNumber of next() calls to reach a value
Given a BST Iterator initialized on the BST below, how many calls to next() are needed to get the value 15?
DSA Python
# BST structure: # 10 # / \ # 5 15 # / \ \ # 3 7 20 root = TreeNode(10, TreeNode(5, TreeNode(3), TreeNode(7)), TreeNode(15, None, TreeNode(20))) iterator = BSTIterator(root) count = 0 while iterator.hasNext(): val = iterator.next() count += 1 if val == 15: break
Attempts:
2 left
💡 Hint
Remember the iterator returns nodes in ascending order.
✗ Incorrect
The in-order traversal order is [3,5,7,10,15,20]. 15 is the 5th element, so 4 calls before it, 5th call returns 15.
❓ Predict Output
expertOutput after mixed next() and hasNext() calls
What is the output of the following code snippet?
DSA Python
root = TreeNode(8, TreeNode(3, TreeNode(1), TreeNode(6)), TreeNode(10, None, TreeNode(14))) iterator = BSTIterator(root) results = [] results.append(iterator.hasNext()) # 1 results.append(iterator.next()) # 2 results.append(iterator.next()) # 3 results.append(iterator.hasNext()) # 4 results.append(iterator.next()) # 5 results.append(iterator.hasNext()) # 6 results.append(iterator.next()) # 7 results.append(iterator.hasNext()) # 8 results.append(iterator.next()) # 9 results.append(iterator.hasNext()) # 10
Attempts:
2 left
💡 Hint
Trace the iterator calls carefully and remember hasNext() returns False only when no nodes remain.
✗ Incorrect
The in-order traversal is [1,3,6,8,10,14]. After 4 next() calls, 10 is returned. The last hasNext() returns True before 14, but after 10, hasNext() is True. After 10 next(), hasNext() is False.
