{"record":{"id":"dd76038769f29079","repo":"krahets/hello-algo","slug":"error-dd7603","errorCode":null,"errorMessage":"佇列已滿","messagePattern":"佇列已滿","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"zh-hant/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/zh-hant/codes/python/chapter_stack_and_queue/array_queue.py#L14-L50","documentation":"Raised by the push() method of a circular-array-based queue when the number of elements equals the allocated capacity. Unlike ArrayDeque (which silently prints and returns on overflow), ArrayQueue raises an IndexError to enforce a hard capacity limit. The capacity is fixed at construction time (len(self._nums)) and does not auto-expand.","triggerScenarios":"Pushing more items than the constructor-specified capacity without popping in between; enqueuing at a rate faster than dequeuing in a producer-consumer setup; using a small capacity (e.g., ArrayQueue(5)) and overfilling it.","commonSituations":"Burst traffic exceeding a fixed buffer size; choosing an undersized capacity for the workload; forgetting that this queue does not auto-grow unlike Python's collections.deque.","solutions":["Check queue.size() < queue.capacity() before calling push()","Increase the capacity argument at construction time to match peak load","Pop before pushing if you need a fixed-size circular buffer that drops old items","Switch to collections.deque if unbounded growth is acceptable"],"exampleFix":"# before\nqueue = ArrayQueue(5)\nfor i in range(10):\n    queue.push(i)  # crashes at i=5\n\n# after\nqueue = ArrayQueue(10)\nfor i in range(10):\n    queue.push(i)\n# or check before push:\nif queue.size() < queue.capacity():\n    queue.push(i)","handlingStrategy":"validation","validationCode":"if queue.size() < queue.capacity():\n    queue.push(num)\nelse:\n    # either increase capacity or drop oldest item\n    queue.pop()\n    queue.push(num)","typeGuard":null,"tryCatchPattern":"try:\n    queue.push(num)\nexcept IndexError:\n    print(\"queue full, dropping item\")","preventionTips":["Choose a capacity at construction time that matches peak expected load","Check size() < capacity() before push() in producer code","This queue does NOT auto-grow; use collections.deque if unbounded capacity is needed"],"tags":["data-structure","queue","python","circular-array","capacity"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}