Challenge - 5 Problems
BST Iterator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of BST Iterator Next Calls
Given a BST with nodes inserted in this order: 7, 3, 15, 9, 20, what is the output of calling Next() three times on a BST iterator initialized with this tree?
DSA Go
type TreeNode struct { Val int Left *TreeNode Right *TreeNode } type BSTIterator struct { stack []*TreeNode } func Constructor(root *TreeNode) BSTIterator { it := BSTIterator{stack: []*TreeNode{}} it.pushLeft(root) return it } func (this *BSTIterator) pushLeft(node *TreeNode) { for node != nil { this.stack = append(this.stack, node) node = node.Left } } func (this *BSTIterator) Next() int { node := this.stack[len(this.stack)-1] this.stack = this.stack[:len(this.stack)-1] this.pushLeft(node.Right) return node.Val } func (this *BSTIterator) HasNext() bool { return len(this.stack) > 0 } // Tree construction root := &TreeNode{7, nil, nil} root.Left = &TreeNode{3, nil, nil} root.Right = &TreeNode{15, nil, nil} root.Right.Left = &TreeNode{9, nil, nil} root.Right.Right = &TreeNode{20, nil, nil} it := Constructor(root) output := []int{it.Next(), it.Next(), it.Next()} fmt.Println(output)
Attempts:
2 left
💡 Hint
Remember, BST Iterator returns nodes in ascending order (inorder traversal).
✗ Incorrect
The BST iterator uses a stack to traverse the tree in ascending order. The first Next() returns the smallest element 3, then 7, then 9.
🧠 Conceptual
intermediate1:30remaining
BST Iterator Space Complexity
What is the maximum space used by a BST Iterator implemented with a stack for a BST with n nodes?
Attempts:
2 left
💡 Hint
Think about the maximum height of the stack during traversal.
✗ Incorrect
The stack stores nodes along the path from root to the current node, which is at most the height of the tree. For a balanced BST, height is O(log n).
🔧 Debug
advanced1:30remaining
Identify the Bug in BST Iterator Next Method
What error will this BST Iterator Next() method cause when called on a non-empty iterator?
DSA Go
func (this *BSTIterator) Next() int { node := this.stack[len(this.stack)] this.stack = this.stack[:len(this.stack)-1] this.pushLeft(node.Right) return node.Val }
Attempts:
2 left
💡 Hint
Check how the stack index is accessed.
✗ Incorrect
The code accesses this.stack[len(this.stack)] which is out of range because slice indices go from 0 to len-1.
❓ Predict Output
advanced2:00remaining
BST Iterator HasNext Behavior
Given a BST iterator initialized with a tree containing nodes 5, 3, 7, what is the output of calling HasNext() four times after three Next() calls?
DSA Go
root := &TreeNode{5, nil, nil}
root.Left = &TreeNode{3, nil, nil}
root.Right = &TreeNode{7, nil, nil}
it := Constructor(root)
results := []bool{}
results = append(results, it.HasNext())
results = append(results, it.Next() == 3)
results = append(results, it.HasNext())
results = append(results, it.Next() == 5)
results = append(results, it.HasNext())
results = append(results, it.Next() == 7)
results = append(results, it.HasNext())
fmt.Println(results)Attempts:
2 left
💡 Hint
HasNext() returns true if there are nodes left to visit.
✗ Incorrect
Initially HasNext() is true. After each Next(), HasNext() remains true until all nodes are visited. After the last Next(), HasNext() returns false.
🧠 Conceptual
expert2:30remaining
BST Iterator Modification for Reverse Order
How would you modify a BST Iterator to return nodes in descending order instead of ascending order?
Attempts:
2 left
💡 Hint
Think about how inorder traversal order changes when visiting right subtree first.
✗ Incorrect
To get descending order, traverse the right subtree first by pushing right children onto the stack before left children.