krahets/hello-algo · error · IndexError

очередь заполнена

Error message

очередь заполнена

What it means

Raised by ArrayQueue#push (ru) when `size == capacity` — this is a FULL condition, not empty. ArrayQueue wraps a fixed-size circular array sized at construction (`initialize(size)`); there is no auto-grow, so push raises IndexError 'очередь заполнена' once the ring is full. This is the ONLY capacity error in the set and reflects the bounded-buffer design.

Source

Thrown at ru/codes/ruby/chapter_stack_and_queue/array_queue.rb:31

  def initialize(size)
    @nums = Array.new(size, 0) # Массив для хранения элементов очереди
    @front = 0 # Указатель head, указывающий на первый элемент очереди
    @size = 0 # Длина очереди
  end

  ### Получить вместимость очереди ###
  def capacity
    @nums.length
  end

  ### Проверка, пуста ли очередь ###
  def is_empty?
    size.zero?
  end

  ### Добавление в очередь ###
  def push(num)
    raise IndexError, 'очередь заполнена' if size == capacity

    # Вычислить указатель хвоста, указывающий на индекс хвоста + 1
    # С помощью операции взятия по модулю вернуть rear к началу после выхода за конец массива
    rear = (@front + size) % capacity
    # Добавить num в хвост очереди
    @nums[rear] = num
    @size += 1
  end

  ### Извлечение из очереди ###
  def pop
    num = peek
    # Указатель head сдвигается на одну позицию назад; если он выходит за конец, то возвращается в начало массива
    @front = (@front + 1) % capacity
    @size -= 1
    num
  end

View on GitHub (pinned to 69932aed18)

Solutions

  1. Check `queue.size < queue.capacity` before push, and backpressure/skip when full.
  2. Construct the queue with a larger capacity up front: `ArrayQueue.new(larger_n)`.
  3. If unbounded growth is required, use a linked-list queue (LinkedListQueue) instead of the fixed ring.
  4. Pop before pushing to free a slot in steady-state ring usage.

Example fix

// before
queue.push(num)  # raises 'очередь заполнена' once full

// after
queue.push(num) unless queue.size == queue.capacity
Defensive patterns

Strategy: validation

Validate before calling

queue.push(num) unless queue.size == queue.capacity

Type guard

def queue_has_room?(q) = q.size < q.capacity

Try / catch

begin
  queue.push(num)
rescue IndexError => e
  raise unless e.message == 'очередь заполнена'
  # apply backpressure: drop, retry, or grow by recreating
end

Prevention

When it happens

Trigger: Calling `queue.push(num)` more than `capacity` times without intervening pops; constructing `ArrayQueue.new(n)` and pushing n+1 items. The circular ring cannot grow past the constructor-given size.

Common situations: Producer faster than consumer in a fixed-capacity queue; sizing the queue too small for the workload; assuming auto-resize like a Ruby Array (it does not).

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/a00ab14b5f8b2e34. Report an issue: GitHub.