Algo-Sim › Low-Level Design

Design Circular Queue

Medium · Low-Level Design · step through it in Java, Python and C++ on your own input

A fixed array with a head index and a count: the rear is (head + count − 1) % k, so slots are reused by wrapping around.

Problem

Design a first-in, first-out queue that holds at most k values in a fixed-size buffer, reusing the space freed at the front once the end is reached. Support enQueue(value) and deQueue(), which return whether they succeeded, Front() and Rear(), which return -1 when the queue is empty, and isEmpty() and isFull().

Constraints

Edge cases to test

Follow-up questions

How do you tell full from empty without a count?
Leave one slot always empty: full when (tail + 1) % k == head.
How would you make it concurrent?
A lock with two condition variables (not full, not empty), or a lock-free ring buffer for one producer and one consumer.

Solution

Java

class MyCircularQueue {
    private final int[] data;
    private int head = 0, count = 0;

    public MyCircularQueue(int k) {
        data = new int[k];
    }

    public boolean enQueue(int value) {
        if (isFull()) return false;
        data[(head + count) % data.length] = value;
        count++;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()) return false;
        head = (head + 1) % data.length;
        count--;
        return true;
    }

    public int Front() {
        return isEmpty() ? -1 : data[head];
    }

    public int Rear() {
        return isEmpty() ? -1 : data[(head + count - 1) % data.length];
    }

    public boolean isEmpty() {
        return count == 0;
    }

    public boolean isFull() {
        return count == data.length;
    }
}

Python

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

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

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

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

    def Rear(self):
        if self.isEmpty(): return -1
        return self.data[(self.head + self.count - 1) % len(self.data)]

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

    def isFull(self):
        return self.count == len(self.data)

C++

#include <bits/stdc++.h>
using namespace std;

class MyCircularQueue {
    vector<int> data;
    int head = 0, count = 0;

  public:
    MyCircularQueue(int k) : data(k) {}

    bool enQueue(int value) {
        if (isFull()) return false;
        data[(head + count) % data.size()] = value;
        count++;
        return true;
    }

    bool deQueue() {
        if (isEmpty()) return false;
        head = (head + 1) % data.size();
        count--;
        return true;
    }

    int Front() {
        return isEmpty() ? -1 : data[head];
    }

    int Rear() {
        return isEmpty() ? -1 : data[(head + count - 1) % data.size()];
    }

    bool isEmpty() {
        return count == 0;
    }

    bool isFull() {
        return count == (int)data.size();
    }
};

More Low-Level Design problems