Stacks and Queues Building stacks and queues
192. Design Circular Queue
A circular queue keeps its values in a fixed array and treats the array as a ring: once the back of the queue
reaches the last place, it carries on from the first place again, if that place is free. Values still leave in
the order they came in. Write a class MyCircularQueue with a constructor and six methods:
MyCircularQueue(k)creates an empty queue with room for at mostkvalues.enQueue(value)addsvalueat the back of the queue and returnstrue. If the queue is full, it changes nothing and returnsfalse.deQueue()removes the value at the front and returnstrue. If the queue is empty, it changes nothing and returnsfalse.Front()returns the value at the front, or-1if the queue is empty.Rear()returns the value at the back, the one added last, or-1if the queue is empty.isEmpty()returnstrueif the queue holds no values.isFull()returnstrueif the queue holdskvalues.
Do not use your language's queue, deque or a list that grows. Keep the values in an array of fixed length, made in the constructor, and make every method run in O(1) time.
Your class is tested by replaying a list of calls on a new MyCircularQueue(k). ops[i] names the i-th call:
"enQueue", "deQueue", "Front", "Rear", "isEmpty" or "isFull". The enQueue calls take their values
from values in order, even when one of them returns false. The answer lists what every call returns, in
order, so it has one entry per call.
In C# and Go the methods are EnQueue, DeQueue, Front, Rear, IsEmpty and IsFull, and in Rust
en_queue, de_queue, front, rear, is_empty and is_full. Go creates the queue with Constructor(k),
and Rust with MyCircularQueue::new(k). The names in ops are the same in every language.
Example 1
Input: k = 3
ops = ["enQueue",
"enQueue",
"enQueue",
"enQueue",
"isFull",
"deQueue",
"enQueue",
"Rear",
"Front"]
values = [5, 8, 1, 4, 4]
[true,true,true,false,true,true,true,4,8]Explanation: The first three values fill the queue, so the first enQueue(4) returns false. deQueue removes 5, the
oldest value. The second enQueue(4) then takes the place 5 left at the start of the array. So 4 is at the
back and 8 is at the front.
Example 2
Input: k = 2
ops = ["isEmpty",
"Front",
"Rear",
"deQueue",
"enQueue",
"Rear",
"Front",
"deQueue",
"isEmpty",
"Rear"]
values = [0]
[true,-1,-1,false,true,0,0,true,true,-1]Explanation: On an empty queue, Front and Rear return -1 and deQueue returns false. The 0 added later is a real
value, so with one value in the queue it is both the front and the back.
Example 3
Input: k = 1
ops = ["enQueue",
"isFull",
"enQueue",
"Rear",
"deQueue",
"enQueue",
"Front",
"isEmpty"]
values = [1000000000, 6, 6]
[true,true,false,1000000000,true,true,6,false]Explanation: With room for one value, the first 6 does not fit. The second 6 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"enQueue","deQueue","Front","Rear","isEmpty"or"isFull". values.lengthequals the number of"enQueue"calls inops.0 ≤ values[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.