{"record":{"id":"1c6bcfefafb4ea86","repo":"krahets/hello-algo","slug":"error-1c6bcf","errorCode":null,"errorMessage":"队列已满","messagePattern":"队列已满","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"codes/python/chapter_stack_and_queue/array_queue.py","lineNumber":32,"sourceCode":"        self._front: int = 0  # 队首指针，指向队首元素\n        self._size: int = 0  # 队列长度\n\n    def capacity(self) -> int:\n        \"\"\"获取队列的容量\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"获取队列的长度\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"判断队列是否为空\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"入队\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"队列已满\")\n        # 计算队尾指针，指向队尾索引 + 1\n        # 通过取余操作实现 rear 越过数组尾部后回到头部\n        rear: int = (self._front + self._size) % self.capacity()\n        # 将 num 添加至队尾\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"出队\"\"\"\n        num: int = self.peek()\n        # 队首指针向后移动一位，若越过尾部，则返回到数组头部\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"访问队首元素\"\"\"\n        if self.is_empty():","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/python/chapter_stack_and_queue/array_queue.py#L14-L50","documentation":"IndexError '队列已满' (queue is full) raised by ArrayQueue.push. This queue is backed by a fixed-capacity circular array; when _size == capacity() there is no free slot, so the guard refuses the write rather than overwriting the front.","triggerScenarios":"Calling queue.push(num) once _size reaches the array length. Also when capacity() is misreported or the caller pushes in a tight loop without consuming.","commonSituations":"Initializing ArrayQueue with a small capacity and feeding more items than it holds; producer/consumer where the producer outruns the consumer; forgetting that this implementation does not auto-grow unlike Python's collections.deque.","solutions":["Check queue.size() < queue.capacity() before push, or wrap with a grow helper.","Size the queue to the worst-case concurrent occupancy at construction.","Consume (pop) before pushing in bounded-buffer scenarios, or use a dynamic queue for unbounded streams."],"exampleFix":"// before\nq.push(x)  # raises once fixed buffer fills\n// after\nif q.size() == q.capacity():\n    q.pop()\nq.push(x)","handlingStrategy":"validation","validationCode":"def safe_push(q, num):\n    if q.size() < q.capacity():\n        q.push(num)\n    else:\n        raise OverflowError(f\"queue full: {q.size()}/{q.capacity()}\")","typeGuard":"def queue_has_room(q) -> bool:\n    return q.size() < q.capacity()","tryCatchPattern":"try:\n    q.push(x)\nexcept IndexError:\n    q.pop()  # evict oldest\n    q.push(x)","preventionTips":["Size the queue to worst-case occupancy at construction.","Check size() < capacity() before push.","Prefer a dynamic queue for unbounded streams; reserve fixed-capacity for back-pressure scenarios."],"tags":["queue","circular-array","index-error","capacity","push"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}