{"record":{"id":"16e12f746c045931","repo":"TheAlgorithms/Java","slug":"minpriorityqueue-is-full-cannot-insert-new-elemen","errorCode":null,"errorMessage":"MinPriorityQueue is full. Cannot insert new element.","messagePattern":"MinPriorityQueue is full\\. Cannot insert new element\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java","lineNumber":46,"sourceCode":"    /**\n     * Initializes a new MinPriorityQueue with a specified capacity.\n     *\n     * @param c the maximum number of elements the queue can hold\n     */\n    public MinPriorityQueue(int c) {\n        this.capacity = c;\n        this.size = 0;\n        this.heap = new int[c + 1];\n    }\n\n    /**\n     * Inserts a new key into the min-priority queue.\n     *\n     * @param key the value to be inserted\n     */\n    public void insert(int key) {\n        if (this.isFull()) {\n            throw new IllegalStateException(\"MinPriorityQueue is full. Cannot insert new element.\");\n        }\n        this.heap[this.size + 1] = key;\n        int k = this.size + 1;\n        while (k > 1) {\n            if (this.heap[k] < this.heap[k / 2]) {\n                int temp = this.heap[k];\n                this.heap[k] = this.heap[k / 2];\n                this.heap[k / 2] = temp;\n            }\n            k = k / 2;\n        }\n        this.size++;\n    }\n\n    /**\n     * Retrieves the highest priority value (the minimum) without removing it.\n     *\n     * @return the minimum value in the queue","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MinPriorityQueue.java#L28-L64","documentation":"Thrown by MinPriorityQueue.insert(int) when isFull() returns true. MinPriorityQueue is backed by a fixed-size int[] of length capacity+1, so insert at size+1 would overflow the array. The library enforces its bounded contract at insertion time.","triggerScenarios":"Inserting more than `capacity` keys into the queue constructed with `new MinPriorityQueue(capacity)`. Inserting in a tight loop without checking isFull.","commonSituations":"Underestimating the required capacity at construction. Feeding an unbounded input stream into a bounded queue. Reusing a queue without draining it first.","solutions":["Check isFull() before insert and drain the queue if full.","Size the queue generously at construction to match expected input volume.","Catch IllegalStateException around insert and grow by creating a larger queue and copying.","Drain the queue with delete() in the producer loop before inserting when full."],"exampleFix":"// before\npq.insert(key); // throws when full\n\n// after\nif (pq.isFull()) {\n    int min = pq.delete(); // make room\n}\npq.insert(key);","handlingStrategy":"validation","validationCode":"if (!pq.isFull()) {\n    pq.insert(key);\n} else {\n    // drain or expand capacity\n}","typeGuard":null,"tryCatchPattern":"try {\n    pq.insert(key);\n} catch (IllegalStateException e) {\n    // queue full — backpressure or grow\n}","preventionTips":["Size the queue at construction to the expected peak load.","Drain the queue in the producer loop when full.","Consider an unbounded alternative if capacity is unknown."],"tags":["priority-queue","capacity","data-structure","bounded","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}