Design Circular Queue

MediumDesignLeetCode 622 ↗World 9-6
0:00 / 0:00

Build a fixed-size ring queue: keep a head index and a count, wrap every step with modulo, and each operation is constant time.

▼

The problem

LeetCode 622 (Medium). Implement MyCircularQueue(k), a FIFO queue with room for k items, with enQueue(value) (add at the back; false if full), deQueue() (remove from the front; false if empty), Front() and Rear() (the first and last items, -1 if empty), isEmpty() and isFull(), each in O(1).

Example (LeetCode's, told in scene 2 and walked through in scene 6): MyCircularQueue(3), then enQueue(1) true, enQueue(2) true, enQueue(3) true, enQueue(4) false, Rear() 3, isFull() true, deQueue() true, enQueue(4) true, Rear() 4.

TRY IT ON LEETCODE ▶

The solution

class MyCircularQueue:
    def __init__(self, k):
        self.q, self.k = [0] * k, k
        self.head = self.count = 0
    def enQueue(self, value):
        if self.count == self.k: return False
        self.q[(self.head + self.count) % self.k] = value
        self.count += 1
        return True
    def deQueue(self):
        if self.count == 0: return False
        self.head = (self.head + 1) % self.k
        self.count -= 1
        return True
    def Front(self):
        if self.count == 0: return -1
        return self.q[self.head]
    def Rear(self):
        if self.count == 0: return -1
        return self.q[(self.head + self.count - 1) % self.k]
    def isEmpty(self): return self.count == 0
    def isFull(self): return self.count == self.k

Transcript

Design Circular Queue. Build a queue that holds at most k items. Enqueue adds at the back, dequeue removes from the front, Front and Rear peek at the two ends, and two checks ask: is it empty, is it full? Every call should take O of one time.

Take k equals three. Enqueue one, two and three: true, true, true. Enqueue four: false, there's no room. Rear gives three, and is it full? True. Dequeue, then enqueue four: true this time, and Rear gives four.

The simple way is a plain list. But dequeue removes index zero, and every item behind it shifts forward one place: O of k work per call. Leaving the front empty instead just wastes it, and the list keeps growing.

The fix: bend the list into a wheel. Keep k fixed slots, a head index for the front, and a count of riders. Nothing moves; only the head goes round. The back is head plus count, mod k, and mod k wraps the last slot round to slot zero. Count zero means empty; count k means full.

In code: enqueue returns false if it's full; otherwise it writes at head plus count, mod k, and adds one to the count. Dequeue returns false if it's empty; otherwise head steps forward one, mod k, and the count drops by one. Front reads the head slot; Rear reads head plus count minus one, mod k.

Let's walk the example. One, two and three board slots zero, one and two. Four finds the count equal to k: full, so false. Rear is slot two, holding three. Dequeue: one gets off, and head moves to slot one. Now four boards at one plus two, mod three: slot zero, wrapping around the wheel. Rear is slot zero, holding four.

Each call is a little arithmetic on one slot: O of one time, and nothing ever shifts. The space is O of k for the slots, plus head and count.

Count the riders, write at head plus count, and wrap with mod k. That's Design Circular Queue.