If the following operations are performed on a Queue, what will be the…
2016
If the following operations are performed on a Queue, what will be the sequence in which the elements are removed?
push(2), push(5), pop(), pop(), push(8), push(6), pop(), pop()
- A.
6, 8, 5, 2
- B.
5, 2, 6, 8
- C.
5, 2, 8, 6
- D.
2, 5, 8, 6
Attempted by 154 students.
Show answer & explanation
Correct answer: D
Concept
A Queue is a linear data structure governed by the FIFO (First In, First Out) principle: the element inserted earliest is the one removed earliest. Insertions add to the rear; removals always take from the front.
Naming note: the standard queue operations are
enqueue()(insert at rear) anddequeue()(remove from front). Herepush()andpop()are used as synonyms for those queue operations, so they still obey FIFO, not the LIFO behaviour those words imply for a stack.
Applying it step by step
Tracing each operation in order, where the front of the queue is on the left:
push(2)→ queue becomes[2].push(5)→5joins the rear →[2, 5](front = 2).pop()→ removes the front;2entered first, so2leaves first. Removed: 2. Queue:[5].pop()→ removes the front;5leaves. Removed: 5. Queue:[].push(8)→[8].push(6)→6joins the rear →[8, 6](front = 8).pop()→8entered first, so it leaves first. Removed: 8. Queue:[6].pop()→6leaves. Removed: 6. Queue:[].
Cross-check and result
Reading the removed elements in the order they left gives 2, 5, 8, 6. This matches FIFO: within each push-push-pop-pop block the earlier-inserted value exits before the later one (2 before 5, then 8 before 6). A reversal within any pair would indicate LIFO (stack) behaviour, which a queue never shows.