What if your waiting line never runs out of space, no matter how many people come or go?
Why Circular Queue Implementation Using Array in DSA Typescript?
Imagine you have a line of people waiting to buy tickets, but the waiting area is very small. When the line reaches the end of the area, you want to start filling empty spots at the beginning without making the line longer. Doing this by hand is confusing and slow.
Trying to manage this line manually means you waste space and time. You might leave empty spots at the start unused while the end is full, causing delays and frustration. Counting and moving people around constantly is tiring and error-prone.
A circular queue using an array solves this by connecting the end of the line back to the start, making a circle. This way, you use all available space efficiently without moving people around. Adding and removing from the queue becomes fast and simple.
let queue = []; // Add elements queue.push(1); queue.push(2); // Remove element queue.shift(); // But space at start is wasted
class CircularQueue { private array: number[]; private front: number; private rear: number; private size: number; constructor(size: number) { this.array = new Array(size); this.front = -1; this.rear = -1; this.size = size; } // enqueue and dequeue methods use circular logic }
It enables efficient use of fixed space to handle continuous data flow without delays or wasted spots.
Think of a printer queue where jobs keep coming and finishing. Using a circular queue helps manage the jobs smoothly without losing track or wasting memory.
Circular queue connects end to start to reuse space.
Prevents wasted space in fixed-size arrays.
Makes adding/removing elements efficient and simple.
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 empty spaces 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 stores different data types automatically
size with front and rear indices?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: Apply modulo for wrap-around
Using modulo ensures the rear wraps to the start of the array, so the condition is (rear + 1) % size === front.Final Answer:
if ((rear + 1) % size === front) -> Option CQuick Check:
Full condition = (rear + 1) % size === front [OK]
- Checking rear === front means empty, not full
- Using rear === size - 1 ignores wrap-around
- Checking front === -1 is for empty queue
front = 0 and rear = 3. After performing enqueue(10) and dequeue(), what are the new values of front and rear?Solution
Step 1: Perform enqueue(10)
rear moves from 3 to (3 + 1) % 5 = 4. So rear = 4, front remains 0.Step 2: Perform dequeue()
front moves from 0 to (0 + 1) % 5 = 1. So front = 1, rear remains 4.Final Answer:
front = 1, rear = 4 -> Option DQuick Check:
Enqueue rear+1=4, Dequeue front+1=1 [OK]
- Not applying modulo for wrap-around
- Updating front on enqueue instead of rear
- Confusing initial and final pointer positions
function dequeue() {
if (front === -1) return null;
const value = queue[front];
if (front === rear) {
front = rear = -1;
} else {
front = front + 1;
}
return value;
}Solution
Step 1: Analyze front update in dequeue
The code updates front as front = front + 1 without modulo, which can exceed array bounds.Step 2: Correct update with modulo
To wrap around in circular queue, front should be updated as front = (front + 1) % size.Final Answer:
Missing modulo operation when updating front, causing index overflow. -> Option AQuick Check:
Front update needs modulo [OK]
- Forgetting modulo causes index errors
- Confusing front and rear updates
- Wrong empty queue condition check
enqueue(5), enqueue(10), enqueue(15), dequeue(), enqueue(20), enqueue(25). What will be the final state of the queue array (index 0 to 3) and the positions of front and rear?Solution
Step 1: Perform enqueues and dequeue step-by-step
Start: front = -1, rear = -1, queue size = 4 - enqueue(5): front=0, rear=0, queue=[5,_,_,_] - enqueue(10): rear=1, queue=[5,10,_,_] - enqueue(15): rear=2, queue=[5,10,15,_] - dequeue(): front=1, queue unchanged - enqueue(20): rear=3, queue=[5,10,15,20] - enqueue(25): rear=(3+1)%4=0, queue[0]=25, queue=[25,10,15,20]Step 2: Final front and rear positions
front=1 (pointing to 10), rear=0 (pointing to 25). Queue array is [25,10,15,20].Final Answer:
Queue: [25, 10, 15, 20], front = 1, rear = 0 -> Option AQuick Check:
Wrap rear with modulo, front moves on dequeue [OK]
- Not wrapping rear index on enqueue
- Not updating front on dequeue
- Mixing up front and rear positions
Ready to prove it?
Pass the test and this pattern moves unknown
