{"record":{"id":"baa5a00821d649e1","repo":"krahets/hello-algo","slug":"queue-is-empty-baa5a0","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"en/codes/python/chapter_stack_and_queue/array_queue.py","lineNumber":51,"sourceCode":"        # 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():\n            raise IndexError(\"Queue is empty\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"Return list for printing\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n\n\n\"\"\"Driver Code\"\"\"\nif __name__ == \"__main__\":\n    # Initialize queue\n    queue = ArrayQueue(10)\n\n    # Elements enqueue","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/python/chapter_stack_and_queue/array_queue.py#L33-L69","documentation":"ArrayQueue.peek raises IndexError('Queue is empty') when _size is zero, blocking the read of self._nums[_front]. Because pop() delegates to peek(), calling pop() on an empty queue surfaces this same exception. The guard enforces that front access is only valid when the queue holds at least one element.","triggerScenarios":"Calling peek() or pop() on a freshly constructed queue; popping after the last element was dequeued; drain loops `while True: q.pop()` without an emptiness exit; consumer running ahead of producer.","commonSituations":"BFS and level-order traversals that dequeue until empty; producer/consumer pacing skew; unguarded counted dequeue loops; test code that pops more than it pushed.","solutions":["Guard single access: `if not queue.is_empty(): head = queue.peek()`.","Bound drains: `while not queue.is_empty(): x = queue.pop()`.","Use queue.size() to control counted loops.","Catch IndexError only when using exception-based flow control deliberately."],"exampleFix":"// before\nhead = queue.peek()  # raises if empty\n// after\nhead = queue.peek() if not queue.is_empty() else None","handlingStrategy":"validation","validationCode":"if not queue.is_empty():\n    head = queue.peek()","typeGuard":"def queue_nonempty(q) -> bool:\n    return not q.is_empty()","tryCatchPattern":"try:\n    head = queue.peek()\nexcept IndexError:\n    head = None","preventionTips":["Drive drains with `while not queue.is_empty()`.","Bound counted loops with size().","Use a wrapper returning Optional for empty-state consumers."],"tags":["queue","indexerror","empty-state","python","data-structures"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}