GATE / CS ARCHIVE

GATE CSE 2014 SET 2 / QUESTION 41

GATE CSE 2014 Set 2 Q41

Data Structures · Stacks & Queues · Full GATE CSE 2014 Set 2 paper

GATE 2014 Set 2, Q41 2 marks · MCQ
Report issue

Stacks & Queues

Question 41, GATE 2014: A stack supports PUSH, POP, and REVERSE, which reverses its current element order. Which statement about implementing a queue is true? A impossible; B ENQUEUE takes one instruction and DEQUEUE two; C ENQUEUE takes three and DEQUEUE one; D both take one.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Set up the reasoning. Choose and maintain an invariant: the queue's front is always at the stack top. Then removing the front is simply POP; a newly enqueued item must be placed at the bottom without disturbing the old order.

2. Derive the result. Keep the queue front at the stack top. ENQUEUE(x) performs REVERSE, PUSH(x), REVERSE, placing x at the bottom while preserving the previous order. DEQUEUE performs POP. Thus three and one instructions respectively, C. A single reversal followed by a pop would leave the remaining stack in the wrong orientation for an arbitrary future operation sequence.

3. Check the interpretation. REVERSE, PUSH, REVERSE inserts at the bottom and restores the invariant. Counting only the reversal and pop needed for one isolated removal ignores the orientation needed by later queue operations.

Original paper, page 40 · Answer key