StreakPeaked· Practice

ExamsGATETechnical

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

  1. A queue cannot be implemented using this stack.
  2. A queue can be implemented where ENQUEUE takes a single instruction and DEQUEUE takes a sequence of two instructions.
  3. A queue can be implemented where ENQUEUE takes a sequence of three instructions and DEQUEUE takes a single instruction.
  4. A queue can be implemented where both ENQUEUE and DEQUEUE take a single instruction each.

Correct answer: A queue can be implemented where ENQUEUE takes a sequence of three instructions and DEQUEUE takes a single instruction.

Solution

With a REVERSE-capable stack you can implement a queue: ENQUEUE = REVERSE, PUSH, REVERSE (three instructions) inserts at the bottom, and DEQUEUE = POP (one instruction) removes the front. Thus option 2 is correct; ENQUEUE and DEQUEUE cannot both be single instructions, so the stored answer (idx 3) is wrong.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →