Circular Queue Implementation Using Array in DSA Javascript - Time & Space Complexity
We want to understand how the time taken by circular queue operations changes as the queue size grows.
How does adding or removing items scale with the number of elements?
Analyze the time complexity of the enqueue and dequeue operations in this circular queue code.
class CircularQueue {
constructor(size) {
this.queue = new Array(size);
this.front = -1;
this.rear = -1;
this.size = size;
}
enqueue(value) {
if ((this.rear + 1) % this.size === this.front) return false; // full
if (this.front === -1) this.front = 0;
this.rear = (this.rear + 1) % this.size;
this.queue[this.rear] = value;
return true;
}
dequeue() {
if (this.front === -1) return null; // empty
const value = this.queue[this.front];
if (this.front === this.rear) {
this.front = -1;
this.rear = -1;
} else {
this.front = (this.front + 1) % this.size;
}
return value;
}
}
This code adds (enqueue) and removes (dequeue) elements in a circular queue using an array.
Look for loops or repeated steps inside enqueue and dequeue.
- Primary operation: Simple index calculations and assignments.
- How many times: Each enqueue or dequeue runs a fixed number of steps once per call.
Each enqueue or dequeue does the same small number of steps no matter how many items are in the queue.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5-7 steps per operation |
| 100 | 5-7 steps per operation |
| 1000 | 5-7 steps per operation |
Pattern observation: The number of steps stays about the same regardless of queue size.
Time Complexity: O(1)
This means each enqueue or dequeue operation takes the same short time no matter how many items are in the queue.
[X] Wrong: "Because the queue uses an array, enqueue or dequeue must check many elements and take longer as the queue grows."
[OK] Correct: The circular queue uses index math to jump directly to the right spot, so it does not scan or shift elements.
Knowing that circular queue operations run in constant time shows you understand efficient data structures for fixed-size buffers.
"What if the queue was implemented with a linked list instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand linear queue limitation
In a linear queue using an array, once the rear reaches the end, no more elements can be added even if there is free space at the front.Step 2: Circular queue advantage
A circular queue connects the end of the array back to the front, allowing reuse of free space and efficient use of the array.Final Answer:
It efficiently uses all available space by wrapping around the array. -> Option BQuick Check:
Circular queue = efficient space use [OK]
- Thinking circular queue sorts elements
- Believing it uses less memory than linear queue
- Assuming it allows mixed data types automatically
size with front and rear pointers?Solution
Step 1: Understand full condition in circular queue
The queue is full when the next position of rear (rear + 1 modulo size) equals front, meaning no space to insert.Step 2: Check each option
if ((rear + 1) % size === front) correctly uses modulo to wrap around and compare with front. Others do not correctly represent full condition.Final Answer:
if ((rear + 1) % size === front) -> Option CQuick Check:
Full condition = (rear + 1) % size === front [OK]
- Checking rear === front for full (actually empty condition)
- Ignoring modulo wrap-around
- Using front === -1 to check full
const size = 5;
let queue = new Array(size);
let front = -1, rear = -1;
function enqueue(value) {
if ((rear + 1) % size === front) {
console.log('Queue is full');
return;
}
if (front === -1) front = 0;
rear = (rear + 1) % size;
queue[rear] = value;
}
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50); // This enqueues successfully (queue now full)
console.log(queue);Solution
Step 1: Trace enqueue operations
Initial: front=-1, rear=-1.
enqueue(10): front=0, rear=0, queue[0]=10.
enqueue(20): rear=1, queue[1]=20.
enqueue(30): rear=2, queue[2]=30.
enqueue(40): rear=3, queue[3]=40.
enqueue(50): (rear+1)%5 = 4 === front 0? No, rear=4, queue[4]=50. No full printed.Step 2: Verify output
5 enqueues succeed for size 5. console.log(queue) shows [10, 20, 30, 40, 50].Final Answer:
[10, 20, 30, 40, 50] -> Option AQuick Check:
Full after exactly size enqueues [OK]
- Thinking queue full after 4 enqueues (5th fails)
- Confusing undefined and null values
- Ignoring front and rear initial values
function dequeue() {
if (front === -1) {
console.log('Queue is empty');
return null;
}
let value = queue[front];
if (front === rear) {
front = rear = -1;
} else {
front = front + 1;
}
return value;
}Solution
Step 1: Analyze front update in dequeue
When removing an element, front moves forward. Since queue is circular, front must wrap using modulo size.Step 2: Identify bug in front update
Code uses front = front + 1 without modulo, so front can go beyond array size causing errors.Final Answer:
front should be updated using modulo to wrap around -> Option AQuick Check:
Update front with modulo to avoid overflow [OK]
- Incrementing rear instead of front on dequeue
- Checking empty condition incorrectly
- Ignoring modulo wrap-around
enqueue(1); enqueue(2); dequeue(); enqueue(3); enqueue(4); dequeue(); enqueue(5);
What is the final state of the queue array?
Solution
Step 1: Trace each operation step-by-step
Start: front=-1, rear=-1, queue=[undefined,undefined,undefined,undefined]
enqueue(1): front=0, rear=0, queue=[1,undefined,undefined,undefined]
enqueue(2): rear=1, queue=[1,2,undefined,undefined]
dequeue(): value=queue[0]=1, front=1 (no clear), queue=[1,2,undefined,undefined]
enqueue(3): rear=2, queue=[1,2,3,undefined]
enqueue(4): rear=3, queue=[1,2,3,4]
dequeue(): value=queue[1]=2, front=2 (no clear), queue=[1,2,3,4]
enqueue(5): rear=0, queue[0]=5, queue=[5,2,3,4]Step 2: Final queue array state
queue=[5,2,3,4]
front=2, rear=0. Dequeue does not clear slots; old values persist until overwritten. Position 1 skipped.Final Answer:
[5, undefined, 3, 4] -> Option DQuick Check:
Wrap-around enqueue updates index 0 after dequeues [OK]
- Not wrapping rear index on enqueue
- Assuming dequeue clears array slots to undefined
- Confusing front and rear after dequeues
Ready to prove it?
Pass the test and this pattern moves unknown
