Watch 10 video solutions for Design Circular Deque, a medium level problem involving Array, Linked List, Design. This walkthrough by codestorywithMIK has 7,669 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Design your implementation of the circular double-ended queue (deque).
Implement the MyCircularDeque class:
MyCircularDeque(int k) Initializes the deque with a maximum size of k.boolean insertFront() Adds an item at the front of Deque. Returns true if the operation is successful, or false otherwise.boolean insertLast() Adds an item at the rear of Deque. Returns true if the operation is successful, or false otherwise.boolean deleteFront() Deletes an item from the front of Deque. Returns true if the operation is successful, or false otherwise.boolean deleteLast() Deletes an item from the rear of Deque. Returns true if the operation is successful, or false otherwise.int getFront() Returns the front item from the Deque. Returns -1 if the deque is empty.int getRear() Returns the last item from Deque. Returns -1 if the deque is empty.boolean isEmpty() Returns true if the deque is empty, or false otherwise.boolean isFull() Returns true if the deque is full, or false otherwise.
Example 1:
Input ["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] [[3], [1], [2], [3], [4], [], [], [], [4], []] Output [null, true, true, true, false, 2, true, true, true, 4] Explanation MyCircularDeque myCircularDeque = new MyCircularDeque(3); myCircularDeque.insertLast(1); // return True myCircularDeque.insertLast(2); // return True myCircularDeque.insertFront(3); // return True myCircularDeque.insertFront(4); // return False, the queue is full. myCircularDeque.getRear(); // return 2 myCircularDeque.isFull(); // return True myCircularDeque.deleteLast(); // return True myCircularDeque.insertFront(4); // return True myCircularDeque.getFront(); // return 4
Constraints:
1 <= k <= 10000 <= value <= 10002000 calls will be made to insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull.Problem Overview: Design a double-ended queue (deque) with a fixed capacity that behaves like a circular buffer. You must support operations such as insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, and isFull in constant time.
Approach 1: Circular Array (O(1) time, O(k) space)
This approach stores elements in a fixed-size array and treats it as circular using modular arithmetic. Maintain two pointers: front and rear. When inserting at the front, decrement the front index using (front - 1 + capacity) % capacity. When inserting at the rear, increment rear using (rear + 1) % capacity. Deletions move the pointers in the opposite direction. The key insight is that wrap‑around indexing lets the array reuse freed positions instead of shifting elements. A counter or size variable helps detect isFull and isEmpty. All operations run in O(1) time with O(k) space, where k is the deque capacity. This approach leverages simple memory layout and cache-friendly access patterns using an array.
Approach 2: Doubly Linked List (O(1) time, O(k) space)
A linked list implementation models the deque with nodes that store prev and next pointers. Maintain two references: head for the front and tail for the rear. Insertions at either end create a new node and adjust the neighboring pointers. Deletions simply move the head or tail pointer and detach the removed node. Because each node directly connects to both neighbors, operations at both ends remain constant time. Track the current size to enforce the capacity limit and determine isFull or isEmpty. Every operation runs in O(1) time with O(k) space for storing nodes. This approach is more flexible but has slightly higher memory overhead due to pointer storage.
Both implementations follow the core behavior of a deque: efficient insertions and removals from both ends. The difference lies in memory layout and pointer management. Circular arrays rely on index arithmetic, while linked lists rely on pointer adjustments.
Recommended for interviews: The circular array implementation is usually the expected answer. It demonstrates understanding of circular buffers, modular arithmetic, and memory efficiency. The linked list approach still satisfies the requirements and shows solid knowledge of pointer manipulation, but interviewers often prefer the array solution because it avoids extra node allocations and mirrors how real systems implement bounded deques.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Circular Array | O(1) per operation | O(k) | Preferred for fixed-capacity deque implementations and interview settings due to simplicity and cache efficiency |
| Doubly Linked List | O(1) per operation | O(k) | Useful when dynamic node manipulation is preferred or when implementing deque behavior without array indexing |