Design Circular Queue

Task example: Design Circular Queue

Implement MyCircularQueue(k) with capacity k and first-in, first-out order. enQueue(value) inserts and returns True, or False when full. deQueue() removes the oldest value and returns True, or False when empty. Front() and Rear() read without removing and return -1 when empty. isEmpty() and isFull() report the state. Do not use a built-in queue.

Example 1
Input
k = 3
enQueue(1), enQueue(2), enQueue(3), enQueue(4),
deQueue(), enQueue(4), Front(), Rear()
Output
[True, True, True, False, True, True, 2, 4]

The fourth insertion fails because the queue is full. Removing 1 makes room for 4. The logical order becomes [2, 3, 4], so Front returns 2 and Rear returns 4.

Example 2
Input
k = 1
deQueue(), Front(), enQueue(0), isFull(), Rear()
Output
[False, -1, True, True, 0]

An empty queue cannot remove or read a value. Zero is a valid value: use size, not slot contents, to detect emptiness.

Capacity is from 1 to 1000, inserted values from 0 to 1000, and there are at most 3000 method calls. The animation allows up to 8 slots and 30 operations for readability.

LeetCode 622 ↗
01 / Baseline: shift values

Shifting array: video explanation

k = 3
enQueue(1), enQueue(2), enQueue(3), enQueue(4),
deQueue(), enQueue(4), Front(), Rear()
[True, True, True, False, True, True, 2, 4]

Prefer reading? Follow the steps below.

  1. Create k slots and set size to zero. The first size slots are active. Insert at index size, unless the queue is full.
  2. To remove the front, copy each remaining value one position left, then decrease size. Reject removal when empty.
  3. Read Front at index 0 and Rear at size - 1. Return -1 when empty. Size 0 means empty; size k means full.

Time complexity · O(k) removal

Initialization takes O(k). deQueue shifts up to k - 1 values, so it takes O(k) in the worst case. All other methods take O(1).

Space complexity · O(k)

The array contains k slots. The counters use O(1) additional space. Here k is the queue capacity.

PYTHONLive code trace
02 / Optimal: circular buffer

Circular buffer: video explanation

k = 3
enQueue(1), enQueue(2), enQueue(3), enQueue(4),
deQueue(), enQueue(4), Front(), Rear()
[True, True, True, False, True, True, 2, 4]

Prefer reading? Follow the steps below.

  1. Create k slots, front = 0, and size = 0. To insert, reject a full queue; otherwise write at (front + size) % k and increase size.
  2. To remove, reject an empty queue; otherwise set front = (front + 1) % k and decrease size. Modulo wraps an index back to 0 at the end.
  3. Read Front at front and Rear at (front + size - 1) % k, unless empty. Old values may stay in inactive slots: only front and size define the logical queue.

Moving an index replaces all the shifts. After wraparound, physical array order can differ from queue order; follow front for exactly size values.

Time complexity · O(1) per operation

Initialization takes O(k). Every queue method takes O(1) worst-case time: it changes only a few indices or values, without shifting.

Space complexity · O(k)

The array contains k slots. The counters use O(1) additional space. Here k is the queue capacity.

PYTHONLive code trace

Design Circular Queue · The task

Close or press Esc to return to your place. The task button stays pinned while you explore the solutions.