Exercises · Q6
Q.Show the status of queue after each operation
enqueue(34)
enqueue(54)
dequeue()
enqueue(12)
dequeue()
enqueue(61)
peek()
dequeue()
dequeue()
dequeue()
dequeue()
enqueue(1)
Yanam BieapTextbookSubjective· 3mImportance★★★★★
71% · 10/14 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →This is a queue trace problem. We simulate a FIFO (First-In-First-Out) queue step by step, showing the front and rear after each operation, and note when operations fail due to an empty queue.
Why a Queue?
A queue is the right data structure here because the problem explicitly asks for queue operations. The key idea: FIFO — the element that has been in the queue the longest (the front) is the one removed by dequeue(). New elements join at the rear. peek() returns the front element without removing it.
We'll maintain a simple list-based queue where:
frontis index 0rearis the last indexenqueue(x)appends to the enddequeue()removes from index 0 (and shifts everything left)peek()returns the element at index 0
Let's trace each operation.
| Operation | Queue State (front → rear) | Front | Rear | Notes |
|---|---|---|---|---|
| Initial | [] | — | — | Empty queue |
enqueue(34) | [34] | 34 | 34 | First element |
enqueue(54) | [34, 54] | 34 | 54 | 54 joins at rear |
dequeue() | [54] | 54 | 54 | Removes 34 (front) |
enqueue(12) | [54, 12] | 54 | 12 | 12 joins at rear |
dequeue() | [12] | 12 | 12 | Removes 54 (front) |
enqueue(61) | [12, 61] | 12 | 61 | 61 joins at rear |
peek() | [12, 61] | 12 | 61 | Returns 12, queue unchanged |
dequeue() | [61] | 61 | 61 | Removes 12 (front) |
dequeue() | [] | — | — | Removes 61 (front), queue now empty |
dequeue() | [] | — | — | Underflow! Queue is empty — operation fails |
dequeue() | [] | — | — | Underflow! Still empty |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.