Design Circular Deque
Design a double-ended queue of fixed capacity k. Support insertFront, insertLast, deleteFront, deleteLast (each returns whether it succeeded), plus getFront and getRear (return the boundary values or -1 when empty), and isEmpty / isFull queries. Insertions fail when full and deletions fail when empty.
Open official problem prompt ↗Build a deque whose front and back both accept and release elements in constant time, without ever reallocating or shifting the underlying storage.
Think of a circular carousel of k seats. Instead of dragging passengers to seat 0, you just move a marker for where the front is; when it runs off the end it loops back to the first seat.
- Input
- MyCircularDeque(3); insertLast(1); insertLast(2); insertFront(3); insertFront(4); getRear(); isFull(); deleteLast(); insertFront(4); getFront()
- Output
- [null, true, true, true, false, 2, true, true, true, 4]
- Why
- After 1,2 at the back and 3 at the front the deque holds [3,1,2] and is full, so insertFront(4) fails; getRear is 2; deleteLast drops 2 leaving room, insertFront(4) makes [4,3,1] and getFront is 4.
1 <= k <= 10000 <= value <= 1000At most 2000 calls total across all methods