← Back to the arrays page

Circular Array (Ring Buffer)

Wrap the end back to the start with modulo indexing. Fixed memory, no shifting — the standard bounded queue.

What to know

  • head and tail chase each other; (i + 1) % n wraps the index.
  • Full vs empty is disambiguated by a count or by sacrificing one slot.
  • Powers-of-two capacity turns modulo into a bit-mask AND.

In the wild: Audio/video buffers, keyboard input queues, log ring buffers (dmesg), producer-consumer channels.

Algorithms to reach for

Circular queue ops

O(1)

O(1) enqueue/dequeue in fixed memory

Circular Kadane

O(n)

Max subarray sum when wrapping is allowed

Next greater element II

O(n)

Monotonic stack over a doubled index range

Practice problems — with full guides

Each problem has its own page: progressive hints, how to approach it, and the full solution.