TheAlgorithms/Java · error · IllegalStateException

Queue is full

Error message

Queue is full

What it means

Thrown by CircularQueue.enQueue(T) as an IllegalStateException when the queue's element count has reached the fixed capacity set at construction. CircularQueue is a bounded, array-backed ring buffer, so inserting beyond capacity is rejected rather than auto-growing. The guard is isFull(), which is true when currentSize == size.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java:76

    /**
     * Checks if the queue is full.
     *
     * @return {@code true} if the queue has reached its maximum capacity; {@code false} otherwise
     */
    public boolean isFull() {
        return currentSize == size;
    }

    /**
     * Adds a new element to the queue. If the queue is full, an exception is thrown.
     *
     * @param value the element to be added to the queue
     * @throws IllegalStateException if the queue is already full
     */
    public void enQueue(T value) {
        if (isFull()) {
            throw new IllegalStateException("Queue is full");
        }
        if (isEmpty()) {
            beginningOfQueue = 0;
        }
        topOfQueue = (topOfQueue + 1) % size;
        array[topOfQueue] = value;
        currentSize++;
    }

    /**
     * Removes and returns the element at the front of the queue.
     *
     * @return the element at the front of the queue
     * @throws IllegalStateException if the queue is empty
     */
    public T deQueue() {
        if (isEmpty()) {
            throw new IllegalStateException("Queue is empty");

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Guard the call with queue.isFull() (or check size vs capacity) and either drain, drop, or reject before enQueue.
  2. Increase the capacity passed to the CircularQueue(int size) constructor to cover peak occupancy.
  3. Pair every enQueue with a corresponding deQueue so the ring buffer stays below capacity.
  4. Switch to an unbounded queue (e.g. java.util.ArrayDeque or a LinkedList) if the workload is bursty and unpredictable.

Example fix

// before
queue.enQueue(item); // throws if full

// after
if (queue.isFull()) {
    queue.deQueue(); // make room
}
queue.enQueue(item);
Defensive patterns

Strategy: validation

Validate before calling

// before enqueuing into a bounded CircularQueue
if (!queue.isFull()) {
    queue.enQueue(item);
} else {
    // drain, drop, or back-pressure
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling enQueue(value) on a CircularQueue constructed with size N after N elements have already been enqueued without matching deQueue() calls. For example, new CircularQueue<>(3) followed by four enQueue() calls throws on the fourth.

Common situations: Producer pushing items faster than consumer drains; capacity sized too small for burst load; forgetting to pair enQueue with deQueue in a request loop; reusing a queue instance across iterations without draining it.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/aae6b210e66cf2bd. Report an issue: GitHub.