Design Circular Queue
Medium
Topics
Design a circular queue using a fixed-size array. Implement MyCircularQueue(k) and: enQueue(value) (returns true on success), deQueue() (returns true on success), Front() and Rear() (return the front/rear value or -1 if empty), isEmpty(), and isFull().
Example 1
A fixed array with head and tail indices that wrap with modulo, so dequeuing never shifts elements. Full and empty otherwise look identical — head equals tail in both — which is why a size counter (or one deliberately unused slot) is needed to tell them apart.
Calls run top to bottom; the arrow shows what each one returns.
Input: operations = ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","Rear","isFull","deQueue","enQueue","Rear"], values = [[3],[1],[2],[3],[4],[],[],[],[4],[]] Output: [null,true,true,true,false,3,true,true,true,4]
Constraints
- 1 <= k <= 1000
- 0 <= value <= 1000
- At most 3000 calls.
Editorial — intuition, approach, complexity and pitfalls
Intuition
A circular queue reuses a fixed array by wrapping indices with modulo, so no shifting is needed. Track a head index and a size; the tail is derived from them.
Approach
- Store a fixed array of capacity k, a head index, and a count.
- enQueue writes at (head + count) % k when not full and bumps the count.
- deQueue advances head modulo k and drops the count; Front/Rear read positions or return -1 when empty.
Dry Run
MyCircularQueue(3). State is a fixed buffer, a head index and a count -- there is
no stored tail.
enQueue 1 -> true buf [1, 0, 0] head 0 count 1
enQueue 2 -> true buf [1, 2, 0] head 0 count 2
enQueue 3 -> true buf [1, 2, 3] head 0 count 3
enQueue 4 -> false full, nothing written
Rear -> 3 index (0 + 3 - 1) % 3 = 2
isFull -> true count == cap
deQueue -> true head 1 count 2 -- the 1 is still in buf, just unreachable
enQueue 4 -> true buf [4, 2, 3] head 1 count 3 -- wrapped to index 0
Rear -> 4 index (1 + 3 - 1) % 3 = 0
answer null|true|true|true|false|3|true|true|true|4
The last enQueue is where the circularity shows. The write index is
(head + count) % cap = (1 + 2) % 3 = 0, so the new 4 lands at the front of the
array while the logical front of the queue is at index 1. Position in the buffer
and position in the queue have come apart, which is the point.
Deriving the tail as (head + count - 1) % cap rather than storing it is what
removes a whole class of bug. A separate mutable tail has to be updated in both
enQueue and deQueue and can desync from head; here there is only one way to be
wrong, and it is in one expression.
Count rather than a "full" flag also settles the classic ambiguity: with only head
and tail, an empty queue and a full queue look identical, since head == tail in
both. Storing the count makes isEmpty and isFull trivially distinguishable.Complexity
- Time — O(1): every operation is constant time via modular indexing.
- Space — O(k): the fixed backing array.
Common Pitfalls
- Compute the tail as (head + count - 1) % cap, not a separate mutable tail that can desync.
Full Solution — all languages
class MyCircularQueue:
def __init__(self, k):
self.buf = [0] * k
self.cap = k
self.head = 0
self.count = 0
def enQueue(self, value):
if self.isFull():
return False
self.buf[(self.head + self.count) % self.cap] = value
self.count += 1
return True
def deQueue(self):
if self.isEmpty():
return False
self.head = (self.head + 1) % self.cap
self.count -= 1
return True
def Front(self):
return -1 if self.isEmpty() else self.buf[self.head]
def Rear(self):
return -1 if self.isEmpty() else self.buf[(self.head + self.count - 1) % self.cap]
def isEmpty(self):
return self.count == 0
def isFull(self):
return self.count == self.capimport java.util.*;
class MyCircularQueue {
private final int[] buf;
private final int cap;
private int head = 0, count = 0;
public MyCircularQueue(int k) {
buf = new int[k];
cap = k;
}
public boolean enQueue(int value) {
if (isFull()) return false;
buf[(head + count) % cap] = value;
count++;
return true;
}
public boolean deQueue() {
if (isEmpty()) return false;
head = (head + 1) % cap;
count--;
return true;
}
public int Front() {
return isEmpty() ? -1 : buf[head];
}
public int Rear() {
return isEmpty() ? -1 : buf[(head + count - 1) % cap];
}
public boolean isEmpty() {
return count == 0;
}
public boolean isFull() {
return count == cap;
}
}#include <bits/stdc++.h>
using namespace std;
class MyCircularQueue {
public: MyCircularQueue(int k) : buf_(k), cap_(k), head_(0), count_(0) {
}
bool enQueue(int value) {
if (isFull()) return false;
buf_[(head_ + count_) % cap_] = value;
count_++;
return true;
}
bool deQueue() {
if (isEmpty()) return false;
head_ = (head_ + 1) % cap_;
count_--;
return true;
}
int Front() {
return isEmpty() ? -1 : buf_[head_];
}
int Rear() {
return isEmpty() ? -1 : buf_[(head_ + count_ - 1) % cap_];
}
bool isEmpty() {
return count_ == 0;
}
bool isFull() {
return count_ == cap_;
}
private: vector<int> buf_;
int cap_, head_, count_;
};class MyCircularQueue {
constructor(k) {
this.buf = new Array(k).fill(0);
this.cap = k;
this.head = 0;
this.count = 0;
}
enQueue(value) {
if (this.isFull()) return false;
this.buf[(this.head + this.count) % this.cap] = value;
this.count++;
return true;
}
deQueue() {
if (this.isEmpty()) return false;
this.head = (this.head + 1) % this.cap;
this.count--;
return true;
}
Front() {
return this.isEmpty() ? -1 : this.buf[this.head];
}
Rear() {
return this.isEmpty() ? -1 : this.buf[(this.head + this.count - 1) % this.cap];
}
isEmpty() {
return this.count === 0;
}
isFull() {
return this.count === this.cap;
}
}