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
- 1 ≤ k ≤ 1000
- 0 ≤ value ≤ 1000
- At most 3000 calls
- Do not use a built-in queue
Edge cases to test
- Full vs empty when head == tail (why a count helps)
- Wrap-around at the end of the array
- k = 1
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
- LRU Cache (Medium)
- LFU Cache (Hard)
- Min Stack (Medium)
- Insert Delete GetRandom O(1) (Medium)
- Flatten Nested List Iterator (Medium)
- Rate Limiter: Token Bucket (Medium)
- Rate Limiter: Sliding Window Log (Medium)
- Logger Rate Limiter (Easy)
- Design Hit Counter (Medium)
- Design Parking System (Easy)
- Design Tic-Tac-Toe (Medium)
- Design Twitter (Medium)
- Design Snake Game (Medium)
- Vending Machine (State pattern) (Medium)
- Elevator System (LOOK scheduling) (Medium)
- Splitwise (expense sharing) (Medium)
- Movie Ticket Booking (BookMyShow) (Medium)