ProgrammingHardTCS NQT · Accenture pattern

What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?

What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?
enqueue 87
enqueue 2
enqueue 71
enqueue 20
dequeue
dequeue
enqueue 57
  1. A. 87, 2, 57
  2. B. 71, 20, 57
  3. C. 71, 20
  4. D. 57, 20, 71

Correct answer: B. 71, 20, 57

A queue is FIFO: dequeue always removes the front (oldest) element. Tracing the sequence gives front-to-back [71, 20, 57]. Treating it like a stack (removing the most recent instead) gives the trap [87, 2, 57]; reading it back-to-front gives [57, 20, 71].

More Pseudocode and Code Tracing questions

Exams that ask this topic