What will be the sequence of numbers, if the following operations are…
2016
What will be the sequence of numbers, if the following operations are performed on a Queue? push(9), pop(), push(5), push(7), pop(), pop(), push(8), pop()
- A.
5, 7, 8, 9
- B.
9, 5, 8, 7
- C.
8, 7, 5, 9
- D.
9, 5, 7, 8
Attempted by 96 students.
Show answer & explanation
Correct answer: D
A Queue works on the FIFO (First-In, First-Out) principle. This means the element that enters the queue first will be the first one to be removed.
push(x)/enqueue(x)adds an element to the rear (back) of the queue.pop()/dequeue()removes and returns the element from the front of the queue.
Let us trace the operations step-by-step to find the sequence of elements that are popped (removed):
Step | Operation | Current State of Queue (Front to Rear) | Popped Element |
1 |
|
| None |
2 |
|
| 9 |
3 |
|
| None |
4 |
|
| None |
5 |
|
| 5 |
6 |
|
| 7 |
7 |
|
| None |
8 |
|
| 8 |
Sequence of popped numbers: 9, 5, 7, 8
Therefore, Option D is the correct answer.