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 Visualization012345FRONTREARKey Operations (All O(1))๐Ÿ”„ Front moves: (front - 1 + capacity) % capacity๐Ÿ”„ Rear moves: (rear + 1) % capacity๐Ÿ“ญ Empty: front == rear๐Ÿ“ฌ Full: (rear + 1) % capacity == front
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.
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