Design Circular Queue
Build a queue of fixed capacity on an array whose end wraps around to its start.
Write a class DesignCircularQueue
- The call
adds a value at the rear and returns true, or returns false if the queue is full.enQueue(value) - The call
removes the value at the front and returns true, or returns false if the queue is empty.deQueue() - The calls
andFront() return the value at the front or at the rear, or -1 if the queue is empty.Rear() - The calls
andisEmpty() tell whether the queue holds no values or k values.isFull()
Example 1
Output: true, true, true, false, 3, true, true, true, 4
The Idea
A queue behaves like the line at a ticket window: people join at the back, called the rear, and are served from the front, in the order they came. In Example 1 the line has room for three values, so they can live in an array of three slots, numbered 0, 1 and 2.
Follow the calls. The first three enQueueenQueue(4)Rear()isFull()deQueue()
Now enQueue(4)
There is a cheaper way. Leave 2 and 3 where they are, remember that the front is now slot 1, and put 4 into the free slot 0. The picture shows both moments, with the front marked in teal and the rear in orange.
In the top row, labelled "after enQueue(1), enQueue(2), enQueue(3)", the front is slot 0 and the rear is slot 2. In the bottom row, labelled "after deQueue() and enQueue(4)", the dashed arrow shows 4 going past slot 2 and on to slot 0. The rear now sits left of the front.
Read the bottom row from the front and carry on past slot 2 back to slot 0: 2, 3, 4. That is the order the values arrived in. The array is used as if its two ends were joined into a ring, which is why the queue is called circular. An array used this way is also known as a ring buffer.
Solution Approach
With the values staying in place, three things remain: what the queue tracks besides the array, how a slot number wraps from 2 back to 0, and how to tell empty from full.
What the queue tracks
Which numbers are enough to find every value? Two of them: frontsizefrontsize
Wrapping the slot number
How does slot 3, one past the end, become slot 0? Java's remainder operator %
So the first free slot is (front + size) % kenQueue(4)frontsize(front + size - 1) % kRear()
Removing a value moves the front one slot on, again with wrapping: front = (front + 1) % kenQueue
Telling empty from full
Why keep a count at all, instead of a second slot number that marks where the queue ends? Suppose that second number is the first free slot. At the start of Example 1, both are slot 0. After the first three enQueuefront
The count removes that doubt: size == 0size == kFront()Rear()deQueue()enQueue
Java Solution
The class has three fields, and each operation is a few lines built on them:
: the k slots, created once in the constructor. Here k isdata .data.length : the slot of the oldest value.front reads it, andFront() moves it one slot on, wrapping.deQueue() : how many values are stored. The checkssize andisEmpty() compare it with 0 and withisFull() .data.length- The two slot formulas:
writes into slotenQueue , and(front + size) % data.length reads slotRear() .(front + size - 1) % data.length
The mainfrontsize
import java.util.ArrayList;
import java.util.List;
public class DesignCircularQueue {
private final int[] data;
private int front = 0; // slot of the oldest value
private int size = 0; // how many values are stored
public DesignCircularQueue(int k) {
data = new int[k];
}
public boolean enQueue(int value) {
if (isFull()) {
return false;
}
data[(front + size) % data.length] = value; // first free slot, wrapping to 0
size++;
return true;
}
public boolean deQueue() {
if (isEmpty()) {
return false;
}
front = (front + 1) % data.length; // the next value becomes the oldest
size--;
return true;
}
public int Front() {
return isEmpty() ? -1 : data[front];
}
public int Rear() {
return isEmpty() ? -1 : data[(front + size - 1) % data.length];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == data.length;
}
public static void main(String[] args) {
DesignCircularQueue q = new DesignCircularQueue(3);
List<Object> out = new ArrayList<>();
out.add(q.enQueue(1));
out.add(q.enQueue(2));
out.add(q.enQueue(3));
out.add(q.enQueue(4)); // full: refused
out.add(q.Rear());
out.add(q.isFull());
out.add(q.deQueue());
out.add(q.enQueue(4)); // goes into slot 0
out.add(q.Rear());
System.out.println(out);
System.out.println("slots " + java.util.Arrays.toString(q.data)
+ ", front " + q.front + ", size " + q.size + ", Front() " + q.Front());
DesignCircularQueue one = new DesignCircularQueue(1);
System.out.println("empty: Front " + one.Front() + ", Rear " + one.Rear()
+ ", deQueue " + one.deQueue() + ", isEmpty " + one.isEmpty());
System.out.println("k = 1: enQueue(7) " + one.enQueue(7) + ", isFull " + one.isFull()
+ ", Front " + one.Front() + ", Rear " + one.Rear() + ", enQueue(8) " + one.enQueue(8));
System.out.println("after deQueue " + one.deQueue() + ": isEmpty " + one.isEmpty()
+ ", Rear " + one.Rear());
}
}
Running it prints:
[true, true, true, false, 3, true, true, true, 4]
slots [4, 2, 3], front 1, size 3, Front() 2
empty: Front -1, Rear -1, deQueue false, isEmpty true
k = 1: enQueue(7) true, isFull true, Front 7, Rear 7, enQueue(8) false
after deQueue true: isEmpty true, Rear -1
The list on the first line, [true, true, true, false, 3, true, true, true, 4][4, 2, 3]frontsizeFront()
The line starting with "empty:" shows the queue with one slot before anything is added: FrontReardeQueueenQueue(8)Rear
In Rear()- 1
Complexity
No operation moves a stored value, so the work per call does not grow with k.
- Time: O(1) per operation, because each one computes at most one slot number with a remainder and reads or writes at most that one slot.
- Space: O(k), because the constructor creates the k slots of
once, anddata andfront are two more numbers, whatever the sequence of calls.size
A queue that also adds and removes at both ends is