Design Circular Queue

Medium

Topics
ArrayQueueDesign

Design a circular queue using a fixed-size array. Implement MyCircularQueue(k) and: enQueue(value) (returns true on success), deQueue() (returns true on success), Front() and Rear() (return the front/rear value or -1 if empty), isEmpty(), and isFull().

Example 1

returnsMyCircularQueue(3)enQueue(1)trueenQueue(2)trueenQueue(3)trueenQueue(4)falseRear()3isFull()truedeQueue()trueenQueue(4)trueRear()4
A fixed array with head and tail indices that wrap with modulo, so dequeuing never shifts elements. Full and empty otherwise look identical — head equals tail in both — which is why a size counter (or one deliberately unused slot) is needed to tell them apart.
Calls run top to bottom; the arrow shows what each one returns.
Input:  operations = ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"], values = [[3],[1],[2],[3],[4],[],[],[],[4],[]]
Output: [null,true,true,true,false,3,true,true,true,4]

Constraints

  • 1 <= k <= 1000
  • 0 <= value <= 1000
  • At most 3000 calls.
Editorial — intuition, approach, complexity and pitfalls

Intuition

A circular queue reuses a fixed array by wrapping indices with modulo, so no shifting is needed. Track a head index and a size; the tail is derived from them.

Approach

  1. Store a fixed array of capacity k, a head index, and a count.
  2. enQueue writes at (head + count) % k when not full and bumps the count.
  3. deQueue advances head modulo k and drops the count; Front/Rear read positions or return -1 when empty.

Dry Run

MyCircularQueue(3). State is a fixed buffer, a head index and a count -- there is
no stored tail.
  enQueue 1   -> true    buf [1, 0, 0]   head 0  count 1
  enQueue 2   -> true    buf [1, 2, 0]   head 0  count 2
  enQueue 3   -> true    buf [1, 2, 3]   head 0  count 3
  enQueue 4   -> false   full, nothing written
  Rear        -> 3       index (0 + 3 - 1) % 3 = 2
  isFull      -> true    count == cap
  deQueue     -> true    head 1  count 2   -- the 1 is still in buf, just unreachable
  enQueue 4   -> true    buf [4, 2, 3]   head 1  count 3   -- wrapped to index 0
  Rear        -> 4       index (1 + 3 - 1) % 3 = 0
answer null|true|true|true|false|3|true|true|true|4
The last enQueue is where the circularity shows. The write index is
(head + count) % cap = (1 + 2) % 3 = 0, so the new 4 lands at the front of the
array while the logical front of the queue is at index 1. Position in the buffer
and position in the queue have come apart, which is the point.
Deriving the tail as (head + count - 1) % cap rather than storing it is what
removes a whole class of bug. A separate mutable tail has to be updated in both
enQueue and deQueue and can desync from head; here there is only one way to be
wrong, and it is in one expression.
Count rather than a "full" flag also settles the classic ambiguity: with only head
and tail, an empty queue and a full queue look identical, since head == tail in
both. Storing the count makes isEmpty and isFull trivially distinguishable.

Complexity

  • Time — O(1): every operation is constant time via modular indexing.
  • Space — O(k): the fixed backing array.

Common Pitfalls

  • Compute the tail as (head + count - 1) % cap, not a separate mutable tail that can desync.

Full Solution — all languages

class MyCircularQueue:
    def __init__(self, k):
        self.buf = [0] * k
        self.cap = k
        self.head = 0
        self.count = 0

    def enQueue(self, value):
        if self.isFull():
            return False
        self.buf[(self.head + self.count) % self.cap] = value
        self.count += 1
        return True

    def deQueue(self):
        if self.isEmpty():
            return False
        self.head = (self.head + 1) % self.cap
        self.count -= 1
        return True

    def Front(self):
        return -1 if self.isEmpty() else self.buf[self.head]

    def Rear(self):
        return -1 if self.isEmpty() else self.buf[(self.head + self.count - 1) % self.cap]

    def isEmpty(self):
        return self.count == 0

    def isFull(self):
        return self.count == self.cap
SensAI
Beta
Tap the mic to stop voice mode