0
0
DSA C++programming~20 mins

BST Iterator Design in DSA C++ - 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 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()
A[3, 7, 9]
B[7, 3, 9]
C[3, 9, 7]
D[7, 9, 15]
Attempts:
2 left
💡 Hint
Remember that BST Iterator returns nodes in ascending order (inorder traversal).
🧠 Conceptual
intermediate
1: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?
AO(n^2), due to repeated stack operations
BO(log n), always regardless of tree shape
CO(n), when the BST is skewed (like a linked list)
DO(1), because it uses constant extra space
Attempts:
2 left
💡 Hint
Consider the maximum height of the stack during traversal.
🔧 Debug
advanced
1: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;
}
ARuntime error: accessing top of empty stack
BCompilation error: missing return statement
CLogical error: returns wrong node value
DNo error, works fine even if stack is empty
Attempts:
2 left
💡 Hint
What happens if next() is called when hasNext() is false?
🚀 Application
advanced
2: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?
A
BSTIterator it(root);
int val = 0;
for (int i = 1; i &lt;= k; i++) {
    val = it.next();
}
return val + 1;
B
BSTIterator it(root);
int val = -1;
for (int i = 0; i &lt; k; i++) {
    val = it.next();
}
return val;
C
BSTIterator it(root);
int count = 0;
while (count &lt; k) {
    it.next();
    count++;
}
return it.next();
D
BSTIterator it(root);
int val = -1;
while (it.hasNext()) {
    val = it.next();
    if (val == k) break;
}
return val;
Attempts:
2 left
💡 Hint
The kth smallest is the value returned by the kth call to next().
Predict Output
expert
2: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()
A[true, 2, 3, true, 5, false]
B[false, 2, 3, true, 5, true]
C[true, 2, 3, false, 5, true]
D[true, 2, 3, true, 5, true]
Attempts:
2 left
💡 Hint
Track the stack and returned values carefully after each call.