Stacks and Queues Building stacks and queues
193. Design Circular Deque
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 mostkvalues.insertFront(value)addsvalueat the front.insertLast(value)adds it at the back. Each returnstrueif the value went in, andfalseif the deque was already full, in which case nothing changes.deleteFront()removes the value at the front, anddeleteLast()removes the value at the back. Each returnstrueif a value was removed, andfalseif the deque was empty.getFront()returns the value at the front, andgetRear()the value at the back, without removing it. Each returns-1if the deque is empty.isEmpty()returns whether the deque holds no values, andisFull()whether it holdskvalues.
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]
[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]
[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]
[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.
Constraints
1 ≤ k ≤ 1051 ≤ ops.length ≤ 2 × 105- Each
ops[i]is one of"insertFront","insertLast","deleteFront","deleteLast","getFront","getRear","isEmpty"and"isFull". values.lengthequals the number of"insertFront"and"insertLast"calls inops.0 ≤ values[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.