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 capacity k
  • insertFront(int value) - Add element at front, return true if successful
  • insertLast(int value) - Add element at rear, return true if successful
  • deleteFront() - Remove front element, return true if successful
  • deleteLast() - Remove rear element, return true if successful
  • getFront() - Get front element, return -1 if empty
  • getRear() - Get rear element, return -1 if empty
  • isEmpty() - Check if deque is empty
  • isFull() - 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).

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]

Visualization

Tap to expand
Circular Deque - Visual Guide INPUT 0 1 2 Capacity: 3 slots Operations: 1. MyCircularDeque(3) 2. insertLast(1) 3. insertLast(2) 4. insertFront(3) 5. insertFront(4) --failed 6. getRear, deleteLast... ALGORITHM STEPS 1 Initialize Array size k+1, front=0, rear=0 2 Insert Front front = (front-1+cap) % cap 3 Insert Last rear = (rear+1) % cap 4 Check Full/Empty Full: (rear+1)%cap == front Index Movement 3 1 2 F R FINAL RESULT Final Deque State: 4 1 2 Front Rear Output Array: [null, true, true, true, false, 2, true, true, true, 4] O(1) All Operations getFront() = 4 [OK] Key Insight: The circular structure uses modular arithmetic to wrap indices around. By using an array of size k+1, we can distinguish between empty (front==rear) and full ((rear+1)%cap==front) states without extra flags. Both ends move in opposite directions: front decrements, rear increments, both wrapping circularly. TutorialsPoint - Design Circular Deque | Optimal Solution
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28 Apple 25
58.2K Views
Medium-High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen