Design a circular queue

A fixed-size queue backed by one array, where the head wraps around with modulo instead of ever shifting elements.

Time O(1) per operationSpace O(n)Code C#, Java, C++ and Python
Watch it animatedStep through it, with the running line highlighted. Free, no sign-in needed to watch.

The idea

A normal queue shifts every element left after a dequeue, or wastes growing space. A circular queue instead keeps a fixed array and lets the logical front, head, walk forward and wrap back to index 0 with modulo. The item at (head + count) % capacity is always the next free slot, so enqueue, dequeue and front are all O(1) with no data ever moved.

Code

public class MyCircularQueue {
    readonly int[] buf;
    int head = 0, count = 0;

    public MyCircularQueue(int capacity) => buf = new int[capacity];

    public bool Enqueue(int x) {
        if (count == buf.Length) return false;
        buf[(head + count) % buf.Length] = x;
        count++;
        return true;
    }

    public bool Dequeue() {
        if (count == 0) return false;
        head = (head + 1) % buf.Length;
        count--;
        return true;
    }

    public int Front() => count == 0 ? -1 : buf[head];
}
static class MyCircularQueue {
    private final int[] buf;
    private int head = 0, count = 0;

    MyCircularQueue(int capacity) { buf = new int[capacity]; }

    boolean enqueue(int x) {
        if (count == buf.length) return false;
        buf[(head + count) % buf.length] = x;
        count++;
        return true;
    }

    boolean dequeue() {
        if (count == 0) return false;
        head = (head + 1) % buf.length;
        count--;
        return true;
    }

    int front() { return count == 0 ? -1 : buf[head]; }
}
class MyCircularQueue {
    vector<int> buf;
    int head = 0, count = 0;
public:
    MyCircularQueue(int capacity) : buf(capacity) {}

    bool enqueue(int x) {
        if (count == (int)buf.size()) return false;
        buf[(head + count) % buf.size()] = x;
        count++;
        return true;
    }

    bool dequeue() {
        if (count == 0) return false;
        head = (head + 1) % buf.size();
        count--;
        return true;
    }

    int front() { return count == 0 ? -1 : buf[head]; }
};
class MyCircularQueue:
    def __init__(self, capacity: int):
        self.buf = [0] * capacity
        self.head = self.count = 0

    def enqueue(self, x: int) -> bool:
        if self.count == len(self.buf): return False
        self.buf[(self.head + self.count) % len(self.buf)] = x
        self.count += 1
        return True

    def dequeue(self) -> bool:
        if self.count == 0: return False
        self.head = (self.head + 1) % len(self.buf)
        self.count -= 1
        return True

    def front(self) -> int:
        return -1 if self.count == 0 else self.buf[self.head]

Tips

In the .NET library

.NET already has Queue<T> as a circular buffer under the hood with amortised O(1) Enqueue/Dequeue; this lesson builds the same idea by hand with a fixed capacity.