Skip to content
Official PYQ · 2001

GATE CS 2001 Q2.16 — Arrays, Stacks and Queues

Explain with AI

What is the minimum number of stacks of size nn required to implement a queue of size nn?

Choose one option
View worked solution

Answer: B

To implement a queue (which follows First-In-First-Out or FIFO order) using stacks (which follow Last-In-First-Out or LIFO order), we need to reverse the order of the elements.

  • With one stack: We cannot reverse the order of elements dynamically to support both enqueue and dequeue operations in a FIFO manner. Thus, one stack is insufficient.
  • With two stacks (S1S_1 and S2S_2): We can successfully simulate a queue of size nn:
    • Enqueue: Push the incoming element onto S1S_1.
    • Dequeue: If S2S_2 is empty, pop all elements from S1S_1 and push them one by one into S2S_2 (this reverses their order, making the oldest element sit at the top of S2S_2). Then, pop the top element from S2S_2. If S2S_2 is not empty, simply pop the top element from S2S_2.

Since the maximum number of elements in the queue at any time is nn, two stacks of size nn are sufficient to handle all operations without overflow.

Therefore, the minimum number of stacks required is two.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions