Skip to content
Official PYQ · 2014

GATE CS 2014 Set 2 Q41 — Arrays, Stacks and Queues

Explain with AI

Suppose a stack implementation supports an instruction REVERSE\text{REVERSE}, which reverses the order of elements on the stack, in addition to the PUSH\text{PUSH} and POP\text{POP} instructions. Which one of the following statements is TRUE\text{TRUE} (with respect to this modified stack)?

Choose one option
View worked solution

Answer: C

To implement a queue (FIFO: First-In-First-Out) using a stack (LIFO: Last-In-First-Out) that supports PUSH, POP, and REVERSE, we need to map the queue operations to stack instructions.

Analyzing Option C:

Suppose we want the DEQUEUE operation to take a single instruction. For this to work, the oldest element in the queue must always reside at the top of the stack so that a simple POP removes it.

Let the current stack state be [c,b,a][c, b, a] (where aa is at the top and is the oldest element, and cc is at the bottom and is the newest).

  • DEQUEUE (1 instruction):
    Simply perform POP.
    Resulting stack: [c,b][c, b] (with bb now at the top).
  • ENQUEUE(x) (3 instructions):
    To insert a new element xx such that it becomes the newest element (at the bottom of the stack), we perform the following sequence:
    1. REVERSE: The stack becomes [b,c][b, c] (with cc at the top).
    2. PUSH(x): The stack becomes [b,c,x][b, c, x] (with xx at the top).
    3. REVERSE: The stack becomes [x,c,b][x, c, b] (with bb at the top).
    Resulting stack: [x,c,b][x, c, b] (where bb is at the top and ready to be dequeued next, and xx is at the bottom).

This implementation requires exactly 3 instructions for ENQUEUE and 1 instruction for DEQUEUE, which perfectly matches statement C.

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

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