Data Structures & AlgorithmsModerateGATE CS · DSA

A queue is implemented with two stacks (push to IN; pop from OUT, refilling OUT from IN only when OUT is empty). Which statement is correct?

  1. A. The amortised cost per operation is Θ(log n)
  2. B. A single dequeue can cost Θ(n), but any sequence of n operations costs O(n) in total
  3. C. Every dequeue costs O(1) in the worst case
  4. D. Every enqueue costs Θ(n)

Correct answer: B. A single dequeue can cost Θ(n), but any sequence of n operations costs O(n) in total

Each element is pushed onto IN once, moved to OUT once and popped once — at most 3 stack operations per element — so the total is linear even though one dequeue may move many elements.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic