What operations does a deque support?
A deque (double-ended queue) supports the following main operations, all in O(1):
- addFront(x): add element
xto the front. - addRear(x): add element
xto the back. - removeFront(): remove and return the element from the front.
- removeRear(): remove and return the element from the back.
- peekFront(): look at the first element without removing it.
- peekRear(): look at the last element without removing it.
- isEmpty(): check whether the queue is empty.
- size(): return the number of elements (sometimes added).
Thanks to access from both sides, a deque can be used both as a queue (FIFO) and as a stack (LIFO).
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.