krahets/hello-algo · error · IndexError

Queue is full

Error message

Queue is full

What it means

Raised by `ArrayQueue#push` (array_queue.rb:31, English version) when `size == capacity`. The queue uses a fixed-capacity circular array with no auto-resize; once full, the rear pointer would collide with `@front`, so the guard prevents overwriting unread elements. Unlike the array deque (which silently drops overflow), the array queue raises.

Source

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

  def initialize(size)
    @nums = Array.new(size, 0) # Array for storing queue elements
    @front = 0 # Front pointer, points to the front of the queue element
    @size = 0 # Queue length
  end

  ### Get queue capacity ###
  def capacity
    @nums.length
  end

  ### Check if queue is empty ###
  def is_empty?
    size.zero?
  end

  ### Enqueue ###
  def push(num)
    raise IndexError, 'Queue is full' if size == capacity

    # Use modulo operation to wrap rear around to the head after passing the tail of the array
    # Add num to the rear of the queue
    rear = (@front + size) % capacity
    # Front pointer moves one position backward
    @nums[rear] = num
    @size += 1
  end

  ### Dequeue ###
  def pop
    num = peek
    # Move front pointer backward by one position, if it passes the tail, return to array head
    @front = (@front + 1) % capacity
    @size -= 1
    num
  end

View on GitHub (pinned to 69932aed18)

Solutions

  1. Size the constructor capacity to peak enqueue depth.
  2. Check `queue.size < queue.capacity` before each push.
  3. Switch to `LinkedListQueue` for unbounded capacity.
  4. Rescue IndexError and apply backpressure (drop/retry).

Example fix

# before
queue.push(item)

# after
queue.push(item) if queue.size < queue.capacity
Defensive patterns

Strategy: validation

Validate before calling

return false if queue.size == queue.capacity
queue.push(item)

Type guard

def queue_pushable?(queue)
  queue.respond_to?(:capacity) && queue.respond_to?(:size) && queue.size < queue.capacity
end

Try / catch

begin
  queue.push(item)
rescue IndexError
  false # backpressure
end

Prevention

When it happens

Trigger: Calling `queue.push(num)` after enqueuing `capacity` elements without dequeueing. The rear index `(@front + size) % capacity` equals `@front`, indicating no free slot.

Common situations: Producer outpacing consumer in a bounded buffer; capacity undersized for burst; forgot to drain between batches; test enqueueing N+1 into capacity-N queue.

Related errors


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