859. Design Circular Deque
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to design a circular double-ended queue (deque) of a fixed size k. A deque is a data structure that supports adding and removing elements from both ends: front and rear. The "circular" nature means that the underlying storage, typically an array, wraps around from the end to the beginning. The core challenge lies in managing the indices for the front and rear of the deque within this fixed-size array, handling the wrap-around logic, and correctly managing the boundary conditions of the deque being empty or full.
Solution
Design Circular Deque in Python
class MyCircularDeque:
def __init__(self, k: int):
"""Initialize your data structure here. Set the size of the deque to be k."""
self.data = [0] * k
self.capacity = k
self.size = 0
# 'front' points to the first element.
# 'rear' points to the next available slot for the last element.
self.front = 0
self.rear = 0
def insertFront(self, value: int) -> bool:
"""Adds an item at the front of Deque. Return true if the operation is successful."""
if self.isFull():
return False
# Move front pointer backwards circularly
self.front = (self.front - 1 + self.capacity) % self.capacity
self.data[self.front] = value
self.size += 1
return True
def insertLast(self, value: int) -> bool:
"""Adds an item at the rear of Deque. Return true if the operation is successful."""
if self.isFull():
return False
# Place value at the current rear position
self.data[self.rear] = value
# Move rear pointer forwards circularly
self.rear = (self.rear + 1) % self.capacity
self.size += 1
return True
def deleteFront(self) -> bool:
"""Deletes an item from the front of Deque. Return true if the operation is successful."""
if self.isEmpty():
return False
# Move front pointer forwards circularly
self.front = (self.front + 1) % self.capacity
self.size -= 1
return True
def deleteLast(self) -> bool:
"""Deletes an item from the rear of Deque. Return true if the operation is successful."""
if self.isEmpty():
return False
# Move rear pointer backwards circularly
self.rear = (self.rear - 1 + self.capacity) % self.capacity
self.size -= 1
return True
def getFront(self) -> int:
"""Get the front item from the deque."""
if self.isEmpty():
return -1
return self.data[self.front]
def getRear(self) -> int:
"""Get the last item from the deque."""
if self.isEmpty():
return -1
# The last element is at the index before 'rear'
last_element_index = (self.rear - 1 + self.capacity) % self.capacity
return self.data[last_element_index]
def isEmpty(self) -> bool:
"""Checks whether the circular deque is empty or not."""
return self.size == 0
def isFull(self) -> bool:
"""Checks whether the circular deque is full or not."""
return self.size == self.capacity
Design Circular Deque in Java
class MyCircularDeque {
private int[] data;
private int front;
private int rear;
private int size;
private int capacity;
/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
data = new int[k];
capacity = k;
size = 0;
// 'front' points to the first element.
// 'rear' points to the next available slot for the last element.
front = 0;
rear = 0;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
// Move front pointer backwards circularly
front = (front - 1 + capacity) % capacity;
data[front] = value;
size++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
// Place value at the current rear position
data[rear] = value;
// Move rear pointer forwards circularly
rear = (rear + 1) % capacity;
size++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
// Move front pointer forwards circularly
front = (front + 1) % capacity;
size--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
// Move rear pointer backwards circularly
rear = (rear - 1 + capacity) % capacity;
size--;
return true;
}
/** Get the front item from the deque. */
public int getFront() {
if (isEmpty()) {
return -1;
}
return data[front];
}
/** Get the last item from the deque. */
public int getRear() {
if (isEmpty()) {
return -1;
}
// The last element is at the index before 'rear'
int lastElementIndex = (rear - 1 + capacity) % capacity;
return data[lastElementIndex];
}
/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
return size == 0;
}
/** Checks whether the circular deque is full or not. */
public boolean isFull() {
return size == capacity;
}
}
Design Circular Deque in C++
class MyCircularDeque {
private:
// Underlying data structure
std::vector<int> data;
// Pointers for front and rear, size and capacity
int front;
int rear;
int size;
int capacity;
public:
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
data.resize(k);
capacity = k;
size = 0;
// 'front' points to the first element.
// 'rear' points to the next available slot for the last element.
front = 0;
rear = 0;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool insertFront(int value) {
if (isFull()) {
return false;
}
// Move front pointer backwards circularly
front = (front - 1 + capacity) % capacity;
data[front] = value;
size++;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool insertLast(int value) {
if (isFull()) {
return false;
}
// Place value at the current rear position
data[rear] = value;
// Move rear pointer forwards circularly
rear = (rear + 1) % capacity;
size++;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if (isEmpty()) {
return false;
}
// Move front pointer forwards circularly
front = (front + 1) % capacity;
size--;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if (isEmpty()) {
return false;
}
// Move rear pointer backwards circularly
rear = (rear - 1 + capacity) % capacity;
size--;
return true;
}
/** Get the front item from the deque. */
int getFront() {
if (isEmpty()) {
return -1;
}
return data[front];
}
/** Get the last item from the deque. */
int getRear() {
if (isEmpty()) {
return -1;
}
// The last element is at the index before 'rear'
int lastElementIndex = (rear - 1 + capacity) % capacity;
return data[lastElementIndex];
}
/** Checks whether the circular deque is empty or not. */
bool isEmpty() {
return size == 0;
}
/** Checks whether the circular deque is full or not. */
bool isFull() {
return size == capacity;
}
};
Design Circular Deque in TypeScript
class MyCircularDeque {
private data: number[];
private capacity: number;
private size: number;
private front: number;
private rear: number;
constructor(k: number) {
this.data = new Array(k);
this.capacity = k;
this.size = 0;
// 'front' points to the first element.
// 'rear' points to the next available slot for the last element.
this.front = 0;
this.rear = 0;
}
insertFront(value: number): boolean {
if (this.isFull()) {
return false;
}
// Move front pointer backwards circularly
this.front = (this.front - 1 + this.capacity) % this.capacity;
this.data[this.front] = value;
this.size++;
return true;
}
insertLast(value: number): boolean {
if (this.isFull()) {
return false;
}
// Place value at the current rear position
this.data[this.rear] = value;
// Move rear pointer forwards circularly
this.rear = (this.rear + 1) % this.capacity;
this.size++;
return true;
}
deleteFront(): boolean {
if (this.isEmpty()) {
return false;
}
// Move front pointer forwards circularly
this.front = (this.front + 1) % this.capacity;
this.size--;
return true;
}
deleteLast(): boolean {
if (this.isEmpty()) {
return false;
}
// Move rear pointer backwards circularly
this.rear = (this.rear - 1 + this.capacity) % this.capacity;
this.size--;
return true;
}
getFront(): number {
if (this.isEmpty()) {
return -1;
}
return this.data[this.front];
}
getRear(): number {
if (this.isEmpty()) {
return -1;
}
// The last element is at the index before 'rear'
const lastElementIndex = (this.rear - 1 + this.capacity) % this.capacity;
return this.data[lastElementIndex];
}
isEmpty(): boolean {
return this.size === 0;
}
isFull(): boolean {
return this.size === this.capacity;
}
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.