krahets/hello-algo · error · IndexError
キューがいっぱいです
Error message
キューがいっぱいです
What it means
This IndexError (Japanese: 'キューがいっぱいです' = 'queue is full') is raised by ArrayQueue.push(num) (array_queue.py:32) when the number of live elements equals the backing array length. ArrayQueue is a fixed-capacity circular-array queue — unlike the deque in this repo, the queue does NOT auto-grow, so pushing past capacity is a hard error. The capacity is the integer passed to the constructor `ArrayQueue(size)`.
Source
Thrown at ja/codes/python/chapter_stack_and_queue/array_queue.py:32
self._front: int = 0 # 先頭ポインタ。先頭要素を指す
self._size: int = 0 # キューの長さ
def capacity(self) -> int:
"""キューの容量を取得"""
return len(self._nums)
def size(self) -> int:
"""キューの長さを取得"""
return self._size
def is_empty(self) -> bool:
"""キューが空かどうかを判定"""
return self._size == 0
def push(self, num: int):
"""エンキュー"""
if self._size == self.capacity():
raise IndexError("キューがいっぱいです")
# 末尾ポインタを計算し、末尾インデックス + 1 を指す
# 剰余演算により、rear が配列末尾を越えた後に先頭へ戻るようにする
rear: int = (self._front + self._size) % self.capacity()
# num をキュー末尾に追加
self._nums[rear] = num
self._size += 1
def pop(self) -> int:
"""デキュー"""
num: int = self.peek()
# 先頭ポインタを1つ後ろへ進め、末尾を越えたら配列先頭に戻す
self._front = (self._front + 1) % self.capacity()
self._size -= 1
return num
def peek(self) -> int:
"""キュー先頭の要素にアクセス"""
if self.is_empty():View on GitHub (pinned to 69932aed18)
Solutions
- Check `if queue.size() < queue.capacity(): queue.push(num)` before pushing.
- Size the constructor capacity to the maximum concurrent elements: `ArrayQueue(max_concurrent)`.
- In a producer/consumer pattern, pop() before push() when at capacity, or use a growable structure.
- Wrap push in try/except IndexError if dropping on full is acceptable.
Example fix
// before
q = ArrayQueue(5)
for i in range(10):
q.push(i) # IndexError on the 6th push
// after
q = ArrayQueue(5)
for i in range(10):
if q.size() == q.capacity():
q.pop()
q.push(i) Defensive patterns
Strategy: validation
Validate before calling
if queue.size() < queue.capacity():
queue.push(num)
else:
# at capacity: pop first, resize at construction, or drop
pass Type guard
def queue_has_room(q: ArrayQueue) -> bool:
return q.size() < q.capacity() Try / catch
try:
queue.push(num)
except IndexError:
# queue full — enqueue failed; apply backpressure or drop
pass Prevention
- Size ArrayQueue(max_concurrent) to the peak number of simultaneous elements.
- Check size() < capacity() before push — the queue does NOT auto-grow.
- In producer/consumer code, pop before push when full, or switch to a growable structure.
- Do not confuse this fixed-capacity queue with Python's unbounded collections.deque.
When it happens
Trigger: Calling push() after `self._size == self.capacity()`. Pushing more than the constructor-supplied size elements without an equal number of pop() calls. Using a small capacity (e.g. ArrayQueue(5)) and pushing 6 times.
Common situations: Underestimating the required capacity at construction time. Producer faster than consumer in a producer/consumer setup with no backpressure check. Confusing this fixed-capacity queue with Python's unbounded collections.deque. The driver loop at the bottom of the file pushes then pops each round to stay within capacity — omitting the pop triggers this.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/a25193b75bd74cae.
Report an issue: GitHub.