Design Circular Queue

Medium · Queue & Deque · about 20 minutes

Asked at Google, Microsoft, Amazon, Bloomberg, NVIDIA, Cloudflare, Hudson River Trading, AMD, Datadog, GitLab, Waymo, Arista Networks, Palo Alto Networks, CrowdStrike, Okta, Vercel, Optiver, Epic Games, Anduril, SpaceX, xAI, Mistral AI, Micron, Arm, Zscaler, Jump Trading, Akamai.

Implement a fixed-size FIFO queue backed by a circular buffer:

  • MyCircularQueue(k) creates a queue with capacity k.
  • enQueue(value) inserts at the rear; returns true on success, false if the queue is full.
  • deQueue() removes the front item; returns true on success, false if the queue is empty.
  • Front() / Rear() return the front / rear item, or -1 if the queue is empty.
  • isEmpty() / isFull() report the queue's state.

Don't use a built-in queue.