GATE Computer ScienceVery hardGATE CS (PYQ pattern)

A queue is built from two stacks: enqueue pushes onto stack IN; dequeue pops from OUT, and if OUT is empty it first moves every element of IN to OUT one by one (a move is one pop and one push). Counting every push and every pop as one primitive operation, how many primitives does the sequence E E E D E E D D E E D D D perform (E = enqueue, D = dequeue)?

  1. A. 27
  2. B. 25
  3. C. 30
  4. D. 33

Correct answer: A. 27

Each enqueue costs 1 (push). Each dequeue costs 1 (pop) plus 2 per element moved at refill. Here 7 enqueues cost 7, 6 dequeues cost 6, and two refills (of 3 and 4 elements) add 2 per moved element = 14; total 27.

More Programming and Data Structures (GATE) questions

Exams that ask this topic