oneby7

Stacks and Queues Building stacks and queues

193. Design Circular Deque

Medium

A deque (double-ended queue, said "deck") is a line of values that can grow and shrink at both ends. Build a deque of integers that holds at most k values. Write a class MyCircularDeque with a constructor and eight methods:

  • MyCircularDeque(k) creates an empty deque that can hold at most k values.
  • insertFront(value) adds value at the front. insertLast(value) adds it at the back. Each returns true if the value went in, and false if the deque was already full, in which case nothing changes.
  • deleteFront() removes the value at the front, and deleteLast() removes the value at the back. Each returns true if a value was removed, and false if the deque was empty.
  • getFront() returns the value at the front, and getRear() the value at the back, without removing it. Each returns -1 if the deque is empty.
  • isEmpty() returns whether the deque holds no values, and isFull() whether it holds k values.

Keep the values in one array of length k, made in the constructor. Do not use a library deque or a list that grows. Each method must run in O(1) time.

Your class is tested by replaying a list of calls on a new MyCircularDeque(k). ops[i] names the i-th call, using the method names above. The inserts take their values from values in order, so the first insertFront or insertLast is given values[0], the next one values[1], and so on, even when an insert fails. Every method returns something, so the answer is the list of what every call returns, in order.

In C# and Go the methods start with a capital letter (InsertFront, GetRear, ...), and in Rust they are in snake case (insert_front, get_rear, ...). Go creates the deque with Constructor(k), and Rust with MyCircularDeque::new(k). The names in ops are the same in every language.

Example 1

Input: k = 3 ops = ["insertLast", "insertFront", "insertFront", "insertLast", "isFull", "getFront", "getRear", "deleteLast", "insertLast", "getRear"] values = [5, 2, 9, 4, 7]

Output: [true,true,true,false,true,9,5,true,true,7]

Explanation: 5 goes in at the back, then 2 and 9 go in at the front, so the deque reads 9, 2, 5 from front to back. It is full, so insertLast(4) fails. deleteLast removes 5, which makes room for 7 at the back.

Example 2

Input: k = 2 ops = ["isEmpty", "getFront", "getRear", "deleteFront", "deleteLast", "insertFront", "getRear", "getFront", "deleteFront", "isEmpty"] values = [0]

Output: [true,-1,-1,false,false,true,0,0,true,true]

Explanation: On an empty deque, both gets return -1 and both deletes return false. The 0 that goes in later is a real value, and with one value in the deque it is both the front and the back.

Example 3

Input: k = 1 ops = ["insertFront", "insertLast", "getRear", "deleteFront", "insertLast", "getFront", "isFull", "deleteLast", "isEmpty"] values = [1000000000, 8, 8]

Output: [true,false,1000000000,true,true,8,true,true,true]

Explanation: With room for one value, the first 8 is turned away. The second 8 goes in only after 1000000000 has left.

Check your understanding

Work out the answer for this input before you write any code.

k = 4 ops = ["insertFront", "insertFront", "insertLast", "getFront", "getRear"] values = [1, 2, 3]

Constraints

  • 1 ≤ k ≤ 105
  • 1 ≤ ops.length ≤ 2 × 105
  • Each ops[i] is one of "insertFront", "insertLast", "deleteFront", "deleteLast", "getFront", "getRear", "isEmpty" and "isFull".
  • values.length equals the number of "insertFront" and "insertLast" calls in ops.
  • 0 ≤ values[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.