Design Circular Queue

Ring Buffer

Design a fixed-capacity first-in, first-out queue on one array: two indexes that wrap around, a size counter that tells full from empty, and every operation in O(1).

Overview

A circular queue, also called a ring buffer, is a queue that lives in one array of fixed size. Elements enter at the back and leave at the front, as in any queue, but instead of shifting everything forward after a dequeue, the queue keeps two indexes that move through the array and wrap to index 0 when they reach the end. The array is treated as a ring.

The interview version asks for enqueue, dequeue, peek, is_empty and is_full, all in O(1), with the capacity fixed when the queue is created. The problem is small, so the interviewer watches the details: the wrap with modular arithmetic, and the one real trap, that a queue whose front and back indexes are equal can be either empty or full. A size counter settles that in one line.

Ring buffers are how bounded buffers are built in practice. Java's ArrayBlockingQueue is an array-backed FIFO queue whose capacity cannot change after construction; Python's collections.deque takes a maxlen and drops from the far end when full; the Linux kernel's kfifo rounds its size up to a power of two so the wrap is a bit mask. Knowing which behavior each one picks when full is a good closing point.

Scoping the problem

The problem is small, so settle its edges before you write code. These are the questions to ask, why each matters, and the usual answer.

  • What happens on enqueue when full? Reject, block, or overwrite the oldest: three different queues. The usual answer is to reject and return False; the others are follow-ups.
  • What happens on dequeue when empty? Return a sentinel or raise. Usually it raises IndexError, as list.pop does.
  • Is the capacity fixed? Yes. A growing ring needs a copy that preserves order.
  • Do we need peek and len? Yes. They are cheap to add, and len needs the size counter.
  • Many threads? A follow-up. It turns the ring into a bounded buffer with a lock and two conditions.
  • What are the elements? Any object. The queue never inspects them.

Functional requirements

  • enqueue(value) adds at the back and returns True, or returns False when the queue is full
  • dequeue() removes and returns the front element, or raises IndexError when empty
  • peek() returns the front element without removing it, or raises IndexError when empty
  • is_empty(), is_full() and len() report the state
  • Capacity is fixed at construction and must be positive
  • Order is first in, first out, across any number of wraps

Non-functional requirements

  • Every operation runs in O(1) time
  • Memory is O(capacity), allocated once; no allocation per element
  • A dequeued slot is cleared so the queue does not keep a reference alive
  • The class is not thread-safe by itself; the bounded-buffer variant adds a lock and conditions

Interview questions

  • Why keep a size counter? Comparing front and back seems enough.
  • How would you make it a bounded buffer for producer and consumer threads?
  • Ring buffer or linked-list queue? When would you pick each?
  • How would you let the queue grow when it is full?
  • What bugs do people write in this problem?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio