Data Structures & AlgorithmsHardProduct-company MCQ pattern

A queue uses two stacks: enqueue pushes onto IN; dequeue pops from OUT, first moving ALL of IN into OUT only if OUT is empty. For enq 1, enq 2, enq 3, deq, enq 4, deq, enq 5, deq, deq, how many single-element moves from IN to OUT happen in total?

  1. A. 7
  2. B. 4
  3. C. 3
  4. D. 5

Correct answer: D. 5

The first deq moves 3 elements (1, 2, 3) and returns 1; the next two deqs find OUT non-empty and return 2, 3; the fourth deq finds OUT empty and moves 4 and 5, then returns 4. Total 3 + 2 = 5. Moving on every dequeue would give more.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic