{"record":{"id":"77a21dc23538b414","repo":"krahets/hello-algo","slug":"queue-is-full","errorCode":null,"errorMessage":"Queue is full","messagePattern":"Queue is full","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"en/codes/python/chapter_stack_and_queue/array_queue.py","lineNumber":32,"sourceCode":"        self._front: int = 0  # Front pointer, points to the front of the queue element\n        self._size: int = 0  # Queue length\n\n    def capacity(self) -> int:\n        \"\"\"Get the capacity of the queue\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"Get the length of the queue\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"Check if the queue is empty\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"Enqueue\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"Queue is full\")\n        # Calculate rear pointer, points to rear index + 1\n        # Use modulo operation to wrap rear around to the head after passing the tail of the array\n        rear: int = (self._front + self._size) % self.capacity()\n        # Add num to the rear of the queue\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Dequeue\"\"\"\n        num: int = self.peek()\n        # Front pointer moves one position backward, if it passes the tail, return to the head of the array\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/python/chapter_stack_and_queue/array_queue.py#L14-L50","documentation":"ArrayQueue.push raises IndexError('Queue is full') when _size equals capacity(), i.e. the fixed-size circular buffer has no free slot. Because the backing array is preallocated to a fixed capacity, enqueueing beyond it would overwrite the front element; the guard rejects the overflow explicitly. This is an overflow precondition, not a transient backpressure signal.","triggerScenarios":"Calling push() more than capacity() times without matching pops; constructing ArrayQueue(capacity) with a too-small capacity for the workload; a producer pushing faster than the consumer pops in a fixed-rate pipeline; ignoring the capacity when sizing the queue.","commonSituations":"Undersized fixed-capacity queues in bounded-buffer producer/consumer code; batch ingest that exceeds the chosen capacity; porting unbounded collections code to a fixed ring buffer without resizing; forgetting that capacity is set once at construction.","solutions":["Check before pushing: `if queue.size() < queue.capacity(): queue.push(x)`.","Size the queue at construction to the worst-case backlog: ArrayQueue(max_concurrent * batch).","Drain (pop) before pushing in tight loops, or apply backpressure to the producer.","If unbounded growth is acceptable, switch to collections.deque instead of the fixed array queue."],"exampleFix":"// before\nqueue.push(item)  # raises when full\n// after\nif queue.size() < queue.capacity():\n    queue.push(item)\nelse:\n    queue.pop()  # make room / apply backpressure","handlingStrategy":"validation","validationCode":"if queue.size() < queue.capacity():\n    queue.push(item)\nelse:\n    queue.pop()  # or apply backpressure","typeGuard":"def queue_has_room(q) -> bool:\n    return q.size() < q.capacity()","tryCatchPattern":"try:\n    queue.push(item)\nexcept IndexError:\n    # queue full; shed load, grow, or wait\n    handle_overflow(item)","preventionTips":["Size the queue at construction to the peak backlog.","Drain (pop) before pushing in tight producer loops.","Switch to collections.deque if unbounded growth is acceptable."],"tags":["queue","indexerror","capacity","overflow","python","data-structures"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}