---
title: "Queue overflow and underflow errors"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:queue-overflow-underflow"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/queue-overflow-underflow
site: BCT Engineering
---

# Queue overflow and underflow errors

Data Structure and Algorithm · Chapter 2: The Stack and Queue

- Asked in: 2073 Magh, 2+3 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/queue-overflow-underflow
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

When do you get error message "Queue overflow and Queue underflow"? Explain with example.

## Short answer

Queue overflow occurs when inserting into a full queue (no space), e.g., enqueue(40) on a size‑3 linear queue already holding 10,20,30. Queue underflow occurs when deleting from an empty queue, e.g., dequeue after all three were removed.

## Answer

A **queue** is a linear data structure in which insertion takes place at one end, the **rear**, and deletion at the other end, the **front**. The element inserted first is removed first, so it follows **FIFO** (First In First Out).

#### Queue overflow

Occurs when we try to **insert into a full queue**, i.e. no space is left. For a linear queue of size N the test is `rear == N - 1`; for a circular queue it is `(rear + 1) % N == front`. The element is not inserted and the message "Queue overflow" is shown.

#### Queue underflow

Occurs when we try to **delete from an empty queue**, i.e. no element exists. For a linear queue the test is `front > rear`; for a circular queue it is `front == -1`. The message "Queue underflow" is shown.

#### Example: linear queue of size 3

| Operation | Array contents (index 0 → N-1) | front | rear |
|---|---|---|---|
| enqueue(10) | 10 _ _ | 0 | 0 |
| enqueue(20) | 10 20 _ | 0 | 1 |
| enqueue(30) | 10 20 30 | 0 | 2 |
| enqueue(40): rear = 2 → Queue overflow | 10 20 30 | 0 | 2 |
| dequeue() returns 10 | _ 20 30 | 1 | 2 |
| dequeue() returns 20 | _ _ 30 | 2 | 2 |
| dequeue() returns 30 | _ _ _ | 3 | 2 |
| dequeue(): front > rear → Queue underflow | _ _ _ | 3 | 2 |

- The fourth `enqueue(40)` finds `rear = 2 = N - 1`, so **overflow** occurs.
- After three dequeues all elements are gone (`front > rear`), so the fourth `dequeue()` causes **underflow**.

Both checks must be made before changing `front` or `rear`, otherwise data outside the array would be read or written.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When does Queue overflow occur?
   A. Deleting from an empty queue
   B. Inserting into a full queue
   C. Inserting into an empty queue
   D. Deleting from a full queue
   Answer: B. Overflow means no space is left for a new element. (Key point: Overflow meaning)

2. When does Queue underflow occur?
   A. Inserting into a full queue
   B. Inserting when front is 0
   C. Deleting from an empty queue
   D. Reading the front element
   Answer: C. Underflow means deletion was tried when no element exists. (Key point: Underflow meaning)

3. For a linear queue of size N, the overflow test is:
   A. front > rear
   B. rear == N - 1
   C. (rear+1)%N == front
   D. front == -1
   Answer: B. When rear is at the last index, no more insertion is possible in a linear queue. (Key point: Linear overflow test)

4. For a linear queue, underflow is detected by:
   A. rear == N - 1
   B. front == rear
   C. front > rear
   D. front == 0
   Answer: C. After all elements are removed front moves beyond rear. (Key point: Linear underflow test)

5. In the size-3 example, after enqueue 10,20,30 and three dequeues, what happens on the next dequeue?
   A. Returns 30 again
   B. Queue underflow
   C. Queue overflow
   D. Returns 0
   Answer: B. front = 3 > rear = 2 so the queue is empty and underflow is reported. (Key point: Example trace)

## Related in Data Structure and Algorithm

- [Resolving underflow in B-tree](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abtree-underflow-balance-combine) — Data Structure and Algorithm, chapter 6
- [Petrol pump queue simulation](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apetrol-pump-simulation) — Data Structure and Algorithm, chapter 2

## Related in other subjects

- [Array overflow/underflow; add two arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-overflow-underflow-add) — Computer Programming, chapter 7
- [Thread errors and effect of pitch error](https://www.bctengineer.com/question.md?key=semester_3%2Fmetrology%3Ascrew-thread-errors-and-pitch-effect) — Metrology, chapter 12
- [Chi-square goodness of fit: Poisson errors](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Achi-square-goodness-fit-poisson-pages) — Probability and Statistics, chapter 7, asked 2×
- [Why program inspections find errors](https://www.bctengineer.com/question.md?key=semester_5%2Fsoftware_engineering%3Aprogram-inspection) — Software Engineering, chapter 7
