Talentd
FleetCode

641. Design Circular Deque

Medium65.2% acceptance5 companies

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 <= 1000
  • 0 <= value <= 1000
  • At most 2000 calls will be made to insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull.
24:55
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class MyCircularDeque:
2
3 def __init__(self, k: int):
4
5
6 def insertFront(self, value: int) -> bool:
7
8
9 def insertLast(self, value: int) -> bool:
10
11
12 def deleteFront(self) -> bool:
13
14
15 def deleteLast(self) -> bool:
16
17
18 def getFront(self) -> int:
19
20
21 def getRear(self) -> int:
22
23
24 def isEmpty(self) -> bool:
25
26
27 def isFull(self) -> bool:
28
29
30
31# Your MyCircularDeque object will be instantiated and called as such:
32# obj = MyCircularDeque(k)
33# param_1 = obj.insertFront(value)
34# param_2 = obj.insertLast(value)
35# param_3 = obj.deleteFront()
36# param_4 = obj.deleteLast()
37# param_5 = obj.getFront()
38# param_6 = obj.getRear()
39# param_7 = obj.isEmpty()
40# param_8 = obj.isFull()
Starter codePython 3Spaces: 4
Examples
Input
["MyCircularDeque","insertLast","insertLast","insertFront","insertFront","getRear","isFull","deleteLast","insertFront","getFront"]
[[3],[1],[2],[3],[4],[],[],[],[4],[]]
Your code is saved on this device.Solution