krahets/hello-algo · error · IndexError
очередь заполнена
Error message
очередь заполнена
What it means
Raised by ArrayQueue.push(num) in chapter_stack_and_queue/array_queue.py:32 — IndexError("очередь заполнена" = "queue is full"). ArrayQueue is a FIXED-capacity ring buffer (no auto-grow); push refuses when _size == capacity(). This is the defining constraint of the type and the one error that is not an empty-state error but a full-state error.
Source
Thrown at ru/codes/python/chapter_stack_and_queue/array_queue.py:32
self._front: int = 0 # Указатель head, указывающий на первый элемент очереди
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()
# Указатель head сдвигается на одну позицию назад; если он выходит за конец, то возвращается в начало массива
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 before push: if q.size() < q.capacity(): q.push(x) else: <backpressure/drop>.
- Construct with a larger capacity sized to peak load.
- Drain (pop) before pushing when full.
- Wrap push in try/except IndexError to implement drop-newest backpressure.
Example fix
// before
q.push(x)
// after
if q.size() < q.capacity():
q.push(x)
else:
q.pop() # drop oldest
q.push(x) Defensive patterns
Strategy: validation
Validate before calling
def safe_push(q, x):
if q.size() < q.capacity():
q.push(x)
return True
return False # apply backpressure Type guard
def queue_has_room(q) -> bool:
return q.size() < q.capacity() Try / catch
try:
q.push(x)
except IndexError:
# queue full — drop oldest, newest, or block
q.pop()
q.push(x) Prevention
- Size capacity to peak load at construction
- This queue does NOT auto-grow — unlike collections.deque
- Implement backpressure (drop-oldest or drop-newest) at the call site
When it happens
Trigger: Pushing more items than the capacity given at construction. A producer outrunning a consumer so the buffer fills. Reusing a queue without draining between batches.
Common situations: Default/under-sized capacity for the workload. Forgetting the queue does not resize (unlike collections.deque). Backpressure not implemented upstream.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/45d59c1b86e9e471.
Report an issue: GitHub.