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 the BST and the iterator code below, what is the output sequence of calling next() four times?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } class BSTIterator { stack: TreeNode[] = []; constructor(root: TreeNode | null) { this.pushLeft(root); } pushLeft(node: TreeNode | null) { while (node !== null) { this.stack.push(node); node = node.left; } } next(): number { const node = this.stack.pop()!; this.pushLeft(node.right); return node.val; } hasNext(): boolean { return this.stack.length > 0; } } // BST structure: // 7 // / \ // 3 15 // / \ // 9 20 const root = new TreeNode(7, new TreeNode(3), new TreeNode(15, new TreeNode(9), new TreeNode(20)) ); const iterator = new BSTIterator(root); const output = []; for (let i = 0; i < 4; i++) { output.push(iterator.next()); } console.log(output);
Attempts:
2 left
💡 Hint
Remember that BST Iterator returns nodes in ascending order using in-order traversal.
✗ Incorrect
The BST Iterator uses a stack to traverse the tree in ascending order. The first next() returns 3 (leftmost), then 7 (root), then 9 (left child of 15), then 15 (parent of 9 and 20).
🧠 Conceptual
intermediate1:00remaining
Understanding BST Iterator hasNext() behavior
What does the hasNext() method of a BST Iterator indicate during iteration?
Attempts:
2 left
💡 Hint
Think about what the stack in the iterator represents.
✗ Incorrect
The hasNext() method checks if the stack has nodes left to visit. If yes, it means there are more nodes to return in ascending order.
🔧 Debug
advanced1:30remaining
Identify the error in BST Iterator next() implementation
What error will occur when running the following next() method code in BST Iterator?
DSA Typescript
next(): number {
const node = this.stack.pop();
this.pushLeft(node.right);
return node.val;
}Attempts:
2 left
💡 Hint
What happens if stack.pop() returns undefined?
✗ Incorrect
If the stack is empty, pop() returns undefined. Accessing node.right or node.val causes a TypeError.
❓ Predict Output
advanced2:00remaining
Output after mixed hasNext() and next() calls
Given the BST Iterator initialized with the BST below, what is the output of the following code?
DSA Typescript
const root = new TreeNode(5, new TreeNode(2), new TreeNode(8, new TreeNode(6), new TreeNode(10)) ); const iterator = new BSTIterator(root); const results = []; while (iterator.hasNext()) { if (iterator.next() % 2 === 0) { results.push(true); } else { results.push(false); } } console.log(results);
Attempts:
2 left
💡 Hint
Check the in-order traversal sequence and test evenness of each value.
✗ Incorrect
In-order traversal: 2 (even → true), 5 (odd → false), 6 (even → true), 8 (even → true), 10 (even → true). Output: [true, false, true, true, true].
🧠 Conceptual
expert1:00remaining
Space complexity of BST Iterator
What is the worst-case space complexity of a BST Iterator implemented with a stack for a BST with n nodes?
Attempts:
2 left
💡 Hint
Consider the maximum number of nodes stored in the stack at once.
✗ Incorrect
The stack stores nodes along the path from root to the current node. The maximum size is the height h of the BST, which can be up to n in worst case (skewed tree).