641. Design Circular Deque

Design Circular Deque (LeetCode 641) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

How to solve Design Circular Deque

Design Circular Deque is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.

Work towards the solution in these steps:

  1. Consider which data structure naturally supports efficient insertion and deletion at both ends while maintaining order.
  2. Think about how to implement the deque using a fixed-size array to achieve the circular behavior, and how to track the front and rear indices.
  3. Focus on how to update the front and rear pointers correctly when inserting or deleting elements, especially when they wrap around the array boundaries.

The reference solution runs in O(1) time and O(k) space.

The Design Circular Deque solution and editorial walks through the full approach with reference code.

Problem statement

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.

Topics and companies

Topics: Array, Linked List, Design, Queue.

Reported in interviews at Amazon.

How to practise Design Circular Deque on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(1) time and O(k) space.

Open Design Circular Deque in the code editor, or read the Design Circular Deque editorial for a worked solution with its approach and complexity analysis.

Where Design Circular Deque sits in the DSA pattern sheet

Problems related to Design Circular Deque

Other problems that use the same Design (General/Specific) and Design Patterns techniques:

Design Circular Deque FAQ

What is the LeetCode number of Design Circular Deque?

Design Circular Deque is LeetCode problem 641, rated Medium.

What pattern is used to solve Design Circular Deque?

Design Circular Deque is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

What is the time complexity of Design Circular Deque?

The reference solution runs in O(1) time and O(k) space.

Which companies ask Design Circular Deque?

Design Circular Deque has been reported in interviews at Amazon.

Preparing this workspace.