High BST Question 167 of 224

How do you implement a BST iterator that returns the next smallest key in average O(1) time?

DSA interview set · Speak this in 60–90 seconds · Faridabad & Delhi NCR

PICTURE THIS: STACK VS QUEUE

StackLIFOlast in, first out
QueueFIFOfirst in, first out

Simple meaning

Push the left spine onto a stack.

1

WHY — BST instead of guessing?

Why interviewers care about BST:

This is a process

question about BST.

Panels listen for order,

trade-offs, and what you would actually do on a DSA project - not buzzwords.

Stay structured

Name the idea, why it exists, then one short example.

Close cleanly

End with when you use it and one common pitfall.

2

STEPS — What happens step by step?

Before you speak the answer, walk the interviewer through these steps:

  1. 1
    Push the left spine

    onto a stack.

  2. 2
    next pops a node

    and pushes the left spine of its right child.

  3. 3
    How it works

    Space is O(h)

  4. 4
    next and hasNext are

    amortized O(1) because each node is pushed and popped once across n calls.

  5. 5
    Flattening all keys up

    front is O(n) space.

  6. 6
    Close

    When you pick this over the alternative.

3

EXAMPLE — See it in action

Here's a short line you can speak, broken into clear beats:

Say this line
“Space is O(h)”
Break into beats
SpaceisOh
Speaking order
29874083374716

Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.

Key takeaway

Push the left spine onto a stack. next pops a node and pushes the left spine of its right child.

Chat with us