TheAlgorithms/Java · error · RuntimeException

Queue is full

Error message

Queue is full

What it means

Thrown by PriorityQueue.insert(int) as a RuntimeException("Queue is full") when isFull(). This is an array-backed max-heap whose capacity is fixed at construction (size+1 slots, with slot 0 unused). Once the heap holds the configured number of items, further inserts are rejected. Note the generic RuntimeException, not a more specific type.

Solutions

  1. Guard with isFull() (or track nItems against capacity) before insert().
  2. Size the constructor capacity to the peak number of concurrent items.
  3. Remove lower-priority elements before inserting new ones to keep occupancy below capacity.
  4. Catch RuntimeException (or the more general Exception) around insert since the type is not specific.

Example fix

// before
pq.insert(value); // throws RuntimeException when full

// after
if (pq.isFull()) {
    pq.remove(); // evict lowest priority to make room
}
pq.insert(value);
Defensive patterns

Strategy: validation

Validate before calling

if (!pq.isFull()) {
    pq.insert(value);
} else {
    pq.remove(); // evict lowest priority
    pq.insert(value);
}

Type guard

null

Try / catch

try { pq.insert(value); } catch (RuntimeException e) { /* capacity handling */ }

Prevention

When it happens

Trigger: Calling insert(value) more than the configured capacity times without remove() calls. Capacity is the size passed to the constructor (default 11); the array is allocated as size+1 but usable capacity equals size.

Common situations: Fixed-capacity priority queue overwhelmed by ingest rate; capacity mis-sized because the +1 internal slot led to an off-by-one in mental model; feed loop with no removal step; RuntimeException not caught because callers only expected checked exceptions.

Related errors


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

Appendix: source

Thrown at src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java:114

            }

            // If not exchange the value of parent with child
            int temp = queueArray[pos];
            queueArray[pos] = queueArray[current];
            queueArray[current] = temp;
            pos = current; // Exchange parent position to child position in the array
        }
    }

    /**
     * Inserts an element in it's appropriate place
     *
     * @param value Value to be inserted
     */
    public void insert(int value) {
        // Print overflow message if the capacity is full
        if (isFull()) {
            throw new RuntimeException("Queue is full");
        } else {
            queueArray[++nItems] = value;
            swim(nItems); // Swim up the element to its correct position
        }
    }

    /**
     * Dequeue the element with the max priority from PQ
     *
     * @return The element removed
     */
    public int remove() {
        if (isEmpty()) {
            throw new RuntimeException("Queue is Empty");
        } else {
            int max = queueArray[1]; // By definition of our max-heap, value at queueArray[1] pos is
                                     // the greatest

View on GitHub (pinned to fdfb9a395b)