{"record":{"id":"9f84386fffd66ddb","repo":"krahets/hello-algo","slug":"queue-is-full-9f8438","errorCode":null,"errorMessage":"Queue is full","messagePattern":"Queue is full","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"en/codes/ruby/chapter_stack_and_queue/array_queue.rb","lineNumber":31,"sourceCode":"  def initialize(size)\n    @nums = Array.new(size, 0) # Array for storing queue elements\n    @front = 0 # Front pointer, points to the front of the queue element\n    @size = 0 # Queue length\n  end\n\n  ### Get queue capacity ###\n  def capacity\n    @nums.length\n  end\n\n  ### Check if queue is empty ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### Enqueue ###\n  def push(num)\n    raise IndexError, 'Queue is full' if size == capacity\n\n    # Use modulo operation to wrap rear around to the head after passing the tail of the array\n    # Add num to the rear of the queue\n    rear = (@front + size) % capacity\n    # Front pointer moves one position backward\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### Dequeue ###\n  def pop\n    num = peek\n    # Move front pointer backward by one position, if it passes the tail, return to array head\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/ruby/chapter_stack_and_queue/array_queue.rb#L13-L49","documentation":"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.","triggerScenarios":"Calling `queue.push(num)` after enqueuing `capacity` elements without dequeueing. The rear index `(@front + size) % capacity` equals `@front`, indicating no free slot.","commonSituations":"Producer outpacing consumer in a bounded buffer; capacity undersized for burst; forgot to drain between batches; test enqueueing N+1 into capacity-N queue.","solutions":["Size the constructor capacity to peak enqueue depth.","Check `queue.size < queue.capacity` before each push.","Switch to `LinkedListQueue` for unbounded capacity.","Rescue IndexError and apply backpressure (drop/retry)."],"exampleFix":"# before\nqueue.push(item)\n\n# after\nqueue.push(item) if queue.size < queue.capacity","handlingStrategy":"validation","validationCode":"return false if queue.size == queue.capacity\nqueue.push(item)","typeGuard":"def queue_pushable?(queue)\n  queue.respond_to?(:capacity) && queue.respond_to?(:size) && queue.size < queue.capacity\nend","tryCatchPattern":"begin\n  queue.push(item)\nrescue IndexError\n  false # backpressure\nend","preventionTips":["Check size < capacity before push.","Size capacity to peak producer depth.","Drain with a consumer loop to stay under capacity.","Use LinkedListQueue if unbounded growth is acceptable."],"tags":["ruby","queue","capacity","bounded-buffer"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}