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 Iterator initialized with the root of the BST below, what is the output sequence of calling next() three times?
DSA C++
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
class BSTIterator {
stack<TreeNode*> st;
void pushLeft(TreeNode* node) {
while (node) {
st.push(node);
node = node->left;
}
}
public:
BSTIterator(TreeNode* root) { pushLeft(root); }
int next() {
TreeNode* node = st.top(); st.pop();
pushLeft(node->right);
return node->val;
}
bool hasNext() { return !st.empty(); }
};
// BST structure:
// 7
// / \
// 3 15
// / \
// 9 20
// Iterator initialized with root (7)
// Calls: next(), next(), next()Attempts:
2 left
💡 Hint
Remember that BST Iterator returns nodes in ascending order (inorder traversal).
✗ Incorrect
The iterator pushes all left nodes first: stack initially has 7, then 3. Calling next() returns 3, then pushes right subtree of 3 (none). Next next() returns 7, then pushes left nodes of 7's right child (15), so stack has 15 and its left child 9. Next next() returns 9.
🧠 Conceptual
intermediate1:30remaining
Space complexity of BST Iterator
What is the worst-case space complexity of the BST Iterator implemented using a stack to simulate inorder traversal on a BST with n nodes?
Attempts:
2 left
💡 Hint
Consider the maximum height of the stack during traversal.
✗ Incorrect
In the worst case, the BST is skewed (all nodes have only one child), so the stack can hold all nodes along the path, which is O(n). For balanced trees, it's O(log n).
🔧 Debug
advanced1:30remaining
Identify the error in BST Iterator next() method
What error will occur when running the following next() method in BST Iterator if the stack is empty?
DSA C++
int next() { TreeNode* node = st.top(); st.pop(); pushLeft(node->right); return node->val; }
Attempts:
2 left
💡 Hint
What happens if next() is called when hasNext() is false?
✗ Incorrect
Calling st.top() on an empty stack causes a runtime error (undefined behavior). The code assumes next() is called only if hasNext() is true.
🚀 Application
advanced2:00remaining
Using BST Iterator to find kth smallest element
Which code snippet correctly uses BST Iterator to find the kth smallest element in a BST?
Attempts:
2 left
💡 Hint
The kth smallest is the value returned by the kth call to next().
✗ Incorrect
Option B calls next() exactly k times and returns the last value, which is the kth smallest. Option B compares value to k incorrectly. Option B calls next() one extra time. Option B adds 1 incorrectly.
❓ Predict Output
expert2:30remaining
Output after mixed hasNext() and next() calls
Given the BST Iterator initialized with the root of the BST below, what is the output of the following calls sequence?
hasNext(), next(), next(), hasNext(), next(), hasNext()
DSA C++
// BST structure: // 5 // / \ // 3 7 // / / \ // 2 6 8 // Calls sequence: // hasNext(), next(), next(), hasNext(), next(), hasNext()
Attempts:
2 left
💡 Hint
Track the stack and returned values carefully after each call.
✗ Incorrect
Initially stack has 5,3,2. hasNext() true. next() returns 2. next() returns 3. hasNext() true (stack has 5). next() returns 5 (pushes 7,6 onto stack). hasNext() true (stack not empty).