Design Circular Deque - Problem
Design and implement a Circular Double-Ended Queue (Deque) data structure that supports insertion and deletion operations from both ends efficiently.
A circular deque is a linear data structure that connects the rear and front ends, forming a circle. This design allows for optimal space utilization by reusing empty slots created after deletions.
Your task: Implement the MyCircularDeque class with the following methods:
MyCircularDeque(int k)- Initialize with maximum capacitykinsertFront(int value)- Add element at front, returntrueif successfulinsertLast(int value)- Add element at rear, returntrueif successfuldeleteFront()- Remove front element, returntrueif successfuldeleteLast()- Remove rear element, returntrueif successfulgetFront()- Get front element, return-1if emptygetRear()- Get rear element, return-1if emptyisEmpty()- Check if deque is emptyisFull()- Check if deque is full
All operations should run in O(1) time complexity!
Input & Output
example_1.py โ Basic Operations
$
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]
๐ก Note:
Initialize deque with capacity 3. Insert 1,2 at rear, then 3 at front. Try to insert 4 at front (fails - full). Get rear element (2). Check if full (true). Delete rear element. Insert 4 at front. Get front element (4).
example_2.py โ Front and Rear Mix
$
Input:
["MyCircularDeque", "insertFront", "getRear", "insertLast", "getFront", "insertFront", "deleteLast", "getRear", "insertLast"]
[[2], [7], [], [5], [], [1], [], [], [3]]
โบ
Output:
[null, true, 7, true, 7, false, true, 7, true]
๐ก Note:
Capacity 2 deque. Insert 7 at front. Get rear (7 - same element). Insert 5 at last. Get front (still 7). Try insert 1 at front (fails - full). Delete last element (5). Get rear (7). Insert 3 at last.
example_3.py โ Edge Cases
$
Input:
["MyCircularDeque", "isEmpty", "insertFront", "isEmpty", "deleteFront", "isEmpty", "getFront", "getRear"]
[[1], [], [1], [], [], [], [], []]
โบ
Output:
[null, true, true, false, true, true, -1, -1]
๐ก Note:
Single capacity deque. Check empty (true). Insert 1 at front. Check empty (false). Delete front. Check empty (true again). Get front/rear from empty deque (both return -1).
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Create empty circular array with front and rear pointers at position 0
2
Insert Front
Move front pointer backward (with wrap-around) and insert element
3
Insert Last
Insert element at rear position and move rear pointer forward
4
Full Detection
Deque is full when (rear + 1) % capacity equals front pointer
5
Wrap Around
Pointers seamlessly wrap from end back to beginning using modulo operation
Key Takeaway
๐ฏ Key Insight: Using modular arithmetic to treat a linear array as circular enables O(1) operations at both ends, making this the optimal solution for deque implementation.
Time & Space Complexity
Time Complexity
O(1)
All operations use direct index access with simple arithmetic
โ Linear Growth
Space Complexity
O(k)
Uses array of size k+1 to store up to k elements
โ Linear Space
Constraints
- 1 โค k โค 1000
- 0 โค value โค 1000
- At most 2000 calls will be made to all methods combined
- All values will be in the range [0, 1000]
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code