oneby7

Stacks and Queues Building stacks and queues

192. Design Circular Queue

Medium

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 most k values.
  • enQueue(value) adds value at the back of the queue and returns true. If the queue is full, it changes nothing and returns false.
  • deQueue() removes the value at the front and returns true. If the queue is empty, it changes nothing and returns false.
  • Front() returns the value at the front, or -1 if the queue is empty.
  • Rear() returns the value at the back, the one added last, or -1 if the queue is empty.
  • isEmpty() returns true if the queue holds no values.
  • isFull() returns true if the queue holds k values.

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]

Output: [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]

Output: [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]

Output: [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.

k = 2 ops = ["enQueue", "enQueue", "deQueue", "enQueue", "Rear", "Front"] values = [7, 9, 3]

Constraints

  • 1 ≤ k ≤ 105
  • 1 ≤ ops.length ≤ 2 × 105
  • Each ops[i] is "enQueue", "deQueue", "Front", "Rear", "isEmpty" or "isFull".
  • values.length equals the number of "enQueue" 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.