0
0
DSA Goprogramming~20 mins

BST Iterator Design in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Iterator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[3, 7, 9]
B[7, 3, 9]
C[7, 9, 15]
D[3, 9, 7]
Attempts:
2 left
💡 Hint
Remember, BST Iterator returns nodes in ascending order (inorder traversal).
🧠 Conceptual
intermediate
1: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?
AO(n), because in the worst case the stack holds all nodes in the tree
BO(log n), because the stack holds nodes along one path from root to leaf
CO(1), because the iterator uses constant extra space
DO(n log n), because the stack stores nodes for each level
Attempts:
2 left
💡 Hint
Think about the maximum height of the stack during traversal.
🔧 Debug
advanced
1: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
}
Aruntime error: nil pointer dereference
Breturns the correct next value
Ccompilation error due to missing return type
Druntime error: index out of range
Attempts:
2 left
💡 Hint
Check how the stack index is accessed.
Predict Output
advanced
2: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)
A[true, true, true, true, false, true, false]
B[true, true, true, true, true, false, false]
C[true, true, true, true, true, true, false]
D[true, true, false, true, true, true, false]
Attempts:
2 left
💡 Hint
HasNext() returns true if there are nodes left to visit.
🧠 Conceptual
expert
2:30remaining
BST Iterator Modification for Reverse Order
How would you modify a BST Iterator to return nodes in descending order instead of ascending order?
AChange the stack push to traverse right children first instead of left children
BUse a queue instead of a stack to store nodes in level order
CModify the tree to swap left and right children before iteration
DReverse the output list after collecting all nodes in ascending order
Attempts:
2 left
💡 Hint
Think about how inorder traversal order changes when visiting right subtree first.