PadhaikoAll subjects

Past exam question: 2066 Bhadra (Regular / Back), Q3(a)

Data Structure and Algorithms · 3 marks

Open in the Padhaiko app

Topic: Queues

Question

Can you always insert an item into an empty queue? Explain with possible reasons and example?

Solution

No — Not in a Naive Array-Based Linear Queue: In a standard array-based linear queue of size MAX using front = 0, rear = -1 (without circular wrap-around or resetting indices when empty), an item cannot be inserted even when the queue is logically empty if rear == MAX - 1.

Reason:

Example (MAX = 3):

  1. Enqueue A, B, C front = 0, rear = 2.
  2. Dequeue A, B, C front = 3, rear = 2 (queue is now empty!).
  3. Attempt enqueue(D): rear == 2 == MAX - 1 Overflow, refusing insertion into an empty queue! (Resolved by resetting front = rear = -1 when empty, or using a circular queue.)
Answer:

No. In a linear array queue without wrap-around or empty-state reset, enqueuing MAX items and then dequeuing them all leaves the queue empty (front > rear) while rear == MAX - 1, triggering false overflow until a circular queue or index reset is used.

All past exam questions for Data Structure and Algorithms