The Design Circular Deque interview question asks you to implement a Double-Ended Queue (Deque) using a fixed-size array. A deque allows adding and removing elements from both the front and the rear. The "circular" requirement means that when you reach the end of the array, you should wrap around to the beginning, making full use of the allocated space.
Companies like Meta and Amazon ask this to test your mastery of Array interview patterns and modulo arithmetic. It evaluates your ability to manage pointers and handle edge cases like "Full" and "Empty" states in a constrained environment. It’s a core systems-programming task.
This problem uses Circular Buffer logic.
front and rear pointers.size variable to easily track if the deque is full or empty.insertFront: front = (front - 1 + capacity) % capacity.insertLast: rear = (rear + 1) % capacity.deleteFront: front = (front + 1) % capacity.deleteLast: rear = (rear - 1 + capacity) % capacity.Capacity = 3.
insertLast(1): [1, _, _]. front=0, rear=0.insertLast(2): [1, 2, _]. front=0, rear=1.insertFront(3): [1, 2, 3]. front = (0-1+3)%3 = 2. rear=1.
getFront(): Returns element at index 2 (value 3).isFull(): Returns true.front when you should be decrementing it (or vice versa).(i - 1) % n can be negative in some languages).Always use a count or size variable. It makes the isEmpty() and isFull() methods trivial and prevents you from having to do complex logic comparing the front and rear pointers.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design Circular Queue | Medium | Solve | |
| Design Front Middle Back Queue | Medium | Solve | |
| Design Phone Directory | Medium | Solve | |
| Zigzag Iterator | Medium | Solve | |
| Moving Average from Data Stream | Easy | Solve |