Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Circular Queue Implementation Using Array in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Circular Queue Implementation Using Array
Initialize queue array with fixed size
↓
Set front = -1, rear = -1
↓
Enqueue operation
↓
Check if queue is full?
↓
Reject enqueue
↓
Update rear pointer
↓
If front == -1, set front = 0
↓
Dequeue operation
↓
Check if queue is empty?
↓
Reject dequeue
↓
Update front pointer
↓
If front passes rear, reset front and rear to -1 (empty)
This flow shows how a circular queue uses a fixed-size array with front and rear pointers that wrap around to reuse space, handling enqueue and dequeue operations with checks for full and empty states.
Execution Sample
DSA Typescript
class CircularQueue {
  constructor(size) {
    this.queue = new Array(size);
    this.front = -1;
    this.rear = -1;
  }
}
This code initializes a circular queue with a fixed size array and sets front and rear pointers to -1 indicating an empty queue.
Execution Table
StepOperationFrontRearQueue ArrayPointer ChangesVisual State
1Initialize queue with size 5-1-1[_, _, _, _, _]front = -1, rear = -1Empty queue
2Enqueue 1000[10, _, _, _, _]rear moves from -1 to 0; front set to 010 -> _ -> _ -> _ -> _
3Enqueue 2001[10, 20, _, _, _]rear moves from 0 to 110 -> 20 -> _ -> _ -> _
4Enqueue 3002[10, 20, 30, _, _]rear moves from 1 to 210 -> 20 -> 30 -> _ -> _
5Dequeue12[10, 20, 30, _, _]front moves from 0 to 110 (removed), 20 -> 30 -> _ -> _
6Enqueue 4013[10, 20, 30, 40, _]rear moves from 2 to 320 -> 30 -> 40 -> _ -> _
7Enqueue 5014[10, 20, 30, 40, 50]rear moves from 3 to 420 -> 30 -> 40 -> 50 -> _
8Enqueue 60 (wrap around)10[60, 20, 30, 40, 50]rear moves from 4 to 0 (wrap)20 -> 30 -> 40 -> 50 -> 60
9Queue Full Check10[60, 20, 30, 40, 50]Cannot enqueue, queue is full20 -> 30 -> 40 -> 50 -> 60 (full)
10Dequeue20[60, 20, 30, 40, 50]front moves from 1 to 220 (removed), 30 -> 40 -> 50 -> 60
11Dequeue30[60, 20, 30, 40, 50]front moves from 2 to 330 (removed), 40 -> 50 -> 60
12Dequeue40[60, 20, 30, 40, 50]front moves from 3 to 440 (removed), 50 -> 60
13Dequeue00[60, 20, 30, 40, 50]front moves from 4 to 0 (wrap)50 (removed), 60
14Dequeue-1-1[60, 20, 30, 40, 50]front == rear, queue becomes empty front = rear = -160 (removed), queue empty
15Dequeue on empty-1-1[60, 20, 30, 40, 50]Cannot dequeue, queue is emptyQueue empty
💡 Execution stops after dequeue on empty queue is rejected.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 10After Step 14Final
front-100011112-1-1
rear-101223400-1-1
queue[_,_,_,_,_][10,_,_,_,_][10,20,_,_,_][10,20,30,_,_][10,20,30,_,_][10,20,30,40,_][10,20,30,40,50][60,20,30,40,50][60,20,30,40,50][60,20,30,40,50][60,20,30,40,50]
Key Moments - 3 Insights
Why does the rear pointer wrap from the end of the array back to the start?
Because the queue is circular, when rear reaches the last index, it wraps to 0 to reuse empty space at the front. See step 8 in execution_table where rear moves from 4 to 0.
How do we know when the queue is full?
The queue is full when moving rear forward would make it equal to front. In step 9, rear is 0 and front is 1, so the queue is full and enqueue is rejected.
What happens when we dequeue the last element?
When front equals rear and we dequeue, the queue becomes empty. Both front and rear reset to -1 as shown in step 14.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what is the position of rear pointer?
A0
B4
C1
D3
💡 Hint
Check the 'Rear' column at step 8 in execution_table.
At which step does the queue become empty after dequeues?
AStep 13
BStep 14
CStep 15
DStep 12
💡 Hint
Look for when front and rear reset to -1 in execution_table.
If we try to enqueue at step 9, what happens?
AElement is added at rear 0
BFront pointer moves forward
CQueue is full, enqueue rejected
DQueue becomes empty
💡 Hint
Step 9 in execution_table shows enqueue rejected due to full queue.
Concept Snapshot
Circular Queue uses a fixed-size array with front and rear pointers.
Enqueue adds at rear, dequeue removes from front.
Pointers wrap around to reuse space (circular).
Queue is empty when front and rear are -1.
Queue is full when next rear equals front.
Wrap-around ensures efficient space use.
Full Transcript
A circular queue uses a fixed-size array and two pointers: front and rear. Initially, both are -1 indicating the queue is empty. When we enqueue, we check if the queue is full by seeing if moving rear forward would meet front. If not full, we add the element at rear and move rear forward, wrapping to 0 if needed. If front was -1, we set it to 0. When we dequeue, we remove the element at front and move front forward, wrapping if needed. If after dequeue front passes rear, the queue is empty and both pointers reset to -1. This wrapping behavior allows efficient reuse of array space. The execution table shows step-by-step how front and rear move, how the array changes, and when enqueue or dequeue operations are rejected due to full or empty states.

Practice

(1/5)
1. What is the main advantage of using a circular queue over a simple linear queue when implemented using an array?
easy
A. It allows storing different data types in the same queue.
B. It efficiently uses all available space by wrapping around the array.
C. It automatically sorts the elements in the queue.
D. It uses less memory than a linear queue.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    It efficiently uses all available space by wrapping around the array. -> Option B
  4. Quick Check:

    Circular queue = efficient space use [OK]
Hint: Circular queue reuses space by wrapping indices [OK]
Common Mistakes:
  • Thinking circular queue sorts elements
  • Believing it uses less memory than linear queue
  • Assuming it stores different data types automatically
2. Which of the following is the correct way to check if a circular queue is full when using an array of size size with front and rear indices?
easy
A. if (rear === front)
B. if (front === -1)
C. if ((rear + 1) % size === front)
D. if (rear === size - 1)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    if ((rear + 1) % size === front) -> Option C
  4. Quick Check:

    Full condition = (rear + 1) % size === front [OK]
Hint: Full when next rear wraps to front [OK]
Common Mistakes:
  • Checking rear === front means empty, not full
  • Using rear === size - 1 ignores wrap-around
  • Checking front === -1 is for empty queue
3. Consider a circular queue of size 5 (indices 0 to 4). Initially, front = 0 and rear = 3. After performing enqueue(10) and dequeue(), what are the new values of front and rear?
medium
A. front = 0, rear = 4
B. front = 1, rear = 3
C. front = 0, rear = 3
D. front = 1, rear = 4

Solution

  1. Step 1: Perform enqueue(10)

    rear moves from 3 to (3 + 1) % 5 = 4. So rear = 4, front remains 0.
  2. Step 2: Perform dequeue()

    front moves from 0 to (0 + 1) % 5 = 1. So front = 1, rear remains 4.
  3. Final Answer:

    front = 1, rear = 4 -> Option D
  4. Quick Check:

    Enqueue rear+1=4, Dequeue front+1=1 [OK]
Hint: Update rear on enqueue, front on dequeue with modulo [OK]
Common Mistakes:
  • Not applying modulo for wrap-around
  • Updating front on enqueue instead of rear
  • Confusing initial and final pointer positions
4. Given the following TypeScript snippet for dequeue operation in a circular queue, identify the bug:
function dequeue() {
  if (front === -1) return null;
  const value = queue[front];
  if (front === rear) {
    front = rear = -1;
  } else {
    front = front + 1;
  }
  return value;
}
medium
A. Missing modulo operation when updating front, causing index overflow.
B. Incorrect check for empty queue; should check rear instead of front.
C. Should update rear instead of front after dequeue.
D. Returning value before updating front causes wrong output.

Solution

  1. Step 1: Analyze front update in dequeue

    The code updates front as front = front + 1 without modulo, which can exceed array bounds.
  2. Step 2: Correct update with modulo

    To wrap around in circular queue, front should be updated as front = (front + 1) % size.
  3. Final Answer:

    Missing modulo operation when updating front, causing index overflow. -> Option A
  4. Quick Check:

    Front update needs modulo [OK]
Hint: Always use modulo when moving front or rear [OK]
Common Mistakes:
  • Forgetting modulo causes index errors
  • Confusing front and rear updates
  • Wrong empty queue condition check
5. You have a circular queue of size 4. You perform these operations in order: 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?
hard
A. Queue: [25, 10, 15, 20], front = 1, rear = 0
B. Queue: [5, 10, 15, 20], front = 1, rear = 3
C. Queue: [20, 25, 15, 10], front = 0, rear = 1
D. Queue: [10, 15, 20, 25], front = 0, rear = 3

Solution

  1. 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]
  2. Step 2: Final front and rear positions

    front=1 (pointing to 10), rear=0 (pointing to 25). Queue array is [25,10,15,20].
  3. Final Answer:

    Queue: [25, 10, 15, 20], front = 1, rear = 0 -> Option A
  4. Quick Check:

    Wrap rear with modulo, front moves on dequeue [OK]
Hint: Track front/rear with modulo after each operation [OK]
Common Mistakes:
  • Not wrapping rear index on enqueue
  • Not updating front on dequeue
  • Mixing up front and rear positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard