TheAlgorithms/Python · error · Exception

QUEUE IS FULL

Error message

QUEUE IS FULL

What it means

CircularQueue.enqueue raises a generic Exception('QUEUE IS FULL') when self.size >= self.n (fixed capacity given at construction). The ring buffer deliberately keeps all n slots usable via modular front/rear indices; the exception is the overflow signal for a non-growing structure.

Source

Thrown at data_structures/queues/circular_queue.py:74

        >>> cq.enqueue("A")  # doctest: +ELLIPSIS
        <data_structures.queues.circular_queue.CircularQueue object at ...>
        >>> (cq.size, cq.first())
        (1, 'A')
        >>> cq.enqueue("B")  # doctest: +ELLIPSIS
        <data_structures.queues.circular_queue.CircularQueue object at ...>
        >>> cq.array
        ['A', 'B', None, None, None]
        >>> (cq.size, cq.first())
        (2, 'A')
        >>> cq.enqueue("C").enqueue("D").enqueue("E")  # doctest: +ELLIPSIS
        <data_structures.queues.circular_queue.CircularQueue object at ...>
        >>> cq.enqueue("F")
        Traceback (most recent call last):
           ...
        Exception: QUEUE IS FULL
        """
        if self.size >= self.n:
            raise Exception("QUEUE IS FULL")

        self.array[self.rear] = data
        self.rear = (self.rear + 1) % self.n
        self.size += 1
        return self

    def dequeue(self):
        """
        This function removes an element from the queue using on self.front value as an
        index and returns it

        >>> cq = CircularQueue(5)
        >>> cq.dequeue()
        Traceback (most recent call last):
           ...
        Exception: UNDERFLOW
        >>> cq.enqueue("A").enqueue("B").dequeue()
        'A'

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check cq.size < cq.n (or compare size to capacity) before enqueueing.
  2. Dequeue before enqueue when full, or block/backpressure the producer.
  3. Size the queue for peak burst depth at construction time.

Example fix

# before
cq.enqueue(item)  # Exception: QUEUE IS FULL
# after
if cq.size < cq.n:
    cq.enqueue(item)
else:
    cq.dequeue()
    cq.enqueue(item)
Defensive patterns

Strategy: validation

Validate before calling

def enqueue_safe(cq, item, drop_oldest=False):
    if cq.size >= cq.n:
        if not drop_oldest:
            return False
        cq.dequeue()
    cq.enqueue(item)
    return True

Try / catch

try:
    cq.enqueue(item)
except Exception as e:
    if 'QUEUE IS FULL' not in str(e):
        raise
    cq.dequeue()
    cq.enqueue(item)

Prevention

When it happens

Trigger: Enqueueing the (n+1)-th item without an intervening dequeue: cq = CircularQueue(5); five enqueues are fine, the sixth raises. Also enqueueing into a queue constructed with a too-small capacity.

Common situations: Producer faster than consumer (bounded-queue backpressure not implemented); capacity taken from config or len(data) minus one by mistake; porting code from queue.Queue which blocks instead of raising.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/8d6108696c456fc8. Report an issue: GitHub.