ProgrammingVery hardInfosys · Capgemini 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 98
enqueue 18
enqueue 3
enqueue 53
enqueue 58
enqueue 39
enqueue 40
dequeue
enqueue 61
enqueue 88
  1. A. 18, 3, 53, 58, 39, 40, 61
  2. B. 18, 3, 53, 58, 39, 40, 61, 88
  3. C. 88, 61, 40, 39, 58, 53, 3, 18
  4. D. 98, 18, 3, 53, 58, 39, 61, 88

Correct answer: B. 18, 3, 53, 58, 39, 40, 61, 88

A queue is FIFO: dequeue always removes the front (oldest) element. Tracing the sequence gives front-to-back [18, 3, 53, 58, 39, 40, 61, 88]. Treating it like a stack (removing the most recent instead) gives the trap [98, 18, 3, 53, 58, 39, 61, 88]; reading it back-to-front gives [88, 61, 40, 39, 58, 53, 3, 18].

More Pseudocode and Code Tracing questions

Exams that ask this topic