Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Circular Queue Implementation Using Array in DSA Javascript - 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 index circularly
↓
If front == -1, set front = 0
↓
Dequeue Operation
↓
Check if queue is empty?
↓
Reject dequeue
↓
Update front index circularly
↓
If front passes rear, reset front and rear to -1
This flow shows how a circular queue uses a fixed-size array with front and rear pointers that wrap around to reuse space.
Execution Sample
DSA Javascript
const size = 5;
let queue = new Array(size);
let front = -1, rear = -1;

// Enqueue 10
// Enqueue 20
// Dequeue
// Enqueue 30
This code initializes a circular queue and performs enqueue and dequeue operations step-by-step.
Execution Table
StepOperationfrontrearQueue ArrayPointer ChangesVisual State
1Initialize queue-1-1[_, _, _, _, _]front = -1, rear = -1Queue is empty
2Enqueue 1000[10, _, _, _, _]rear moves to 0, front set to 010 -> _ -> _ -> _ -> _ (front=0, rear=0)
3Enqueue 2001[10, 20, _, _, _]rear moves to 110 -> 20 -> _ -> _ -> _ (front=0, rear=1)
4Dequeue11[10, 20, _, _, _]front moves to 110 (removed) -> 20 -> _ -> _ -> _ (front=1, rear=1)
5Enqueue 3012[10, 20, 30, _, _]rear moves to 220 -> 30 -> _ -> _ -> _ (front=1, rear=2)
6Enqueue 4013[10, 20, 30, 40, _]rear moves to 320 -> 30 -> 40 -> _ -> _ (front=1, rear=3)
7Enqueue 5014[10, 20, 30, 40, 50]rear moves to 420 -> 30 -> 40 -> 50 -> _ (front=1, rear=4)
8Enqueue 6010[60, 20, 30, 40, 50]rear wraps to 020 -> 30 -> 40 -> 50 -> 60 (front=1, rear=0)
9Enqueue 70 (Full)10[60, 20, 30, 40, 50]No changeQueue is full, enqueue rejected
10Dequeue20[60, 20, 30, 40, 50]front moves to 230 -> 40 -> 50 -> 60 -> _ (front=2, rear=0)
11Dequeue30[60, 20, 30, 40, 50]front moves to 340 -> 50 -> 60 -> _ -> _ (front=3, rear=0)
12Dequeue40[60, 20, 30, 40, 50]front moves to 450 -> 60 -> _ -> _ -> _ (front=4, rear=0)
13Dequeue00[60, 20, 30, 40, 50]front wraps to 060 -> _ -> _ -> _ -> _ (front=0, rear=0)
14Dequeue (Empty)-1-1[60, 20, 30, 40, 50]front and rear reset to -1Queue is empty
15Exit-1-1[60, 20, 30, 40, 50]No operationQueue is empty, no more operations
💡 Execution stops after queue becomes empty and no more operations remain.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11After Step 12After Step 13After Step 14
front-1001111112340-1
rear-1011234000000-1
queue[_,_,_,_,_][10,_,_,_,_][10,20,_,_,_][10,20,_,_,_][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][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 back to 0 after reaching the end of the array?
Because the queue is circular, when rear reaches the last index, it wraps to 0 to reuse empty space at the start, as shown in step 8 where rear moves from 4 to 0.
Why do we reset front and rear to -1 when the queue becomes empty?
Resetting front and rear to -1 indicates the queue is empty. This happens in step 14 after dequeuing the last element, so the queue state is clear for future operations.
How do we know when the queue is full and cannot enqueue more elements?
The queue is full when the next rear position equals front. In step 9, rear is 0 and front is 1, so the queue is full and enqueue is rejected.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 8. What is the value of rear and why?
Arear = 5 because it moved forward linearly
Brear = 0 because it wrapped around after reaching the end
Crear = 4 because it did not move
Drear = -1 because queue is empty
💡 Hint
Check the 'rear' column at step 8 and see how rear moves circularly in the queue.
At which step does the queue become empty again after several operations?
AStep 14
BStep 12
CStep 13
DStep 15
💡 Hint
Look at the 'front' and 'rear' values resetting to -1 in the execution table.
If we try to enqueue at step 9, what happens and why?
AQueue resets automatically
BEnqueue succeeds and rear moves forward
CEnqueue fails because queue is full
DFront pointer moves instead of rear
💡 Hint
Check the 'Operation' and 'Visual State' columns at step 9 in the execution table.
Concept Snapshot
Circular Queue uses a fixed-size array with two pointers: front and rear.
Enqueue adds at rear; dequeue removes from front.
Pointers wrap around when reaching array end (circular).
Queue is full when next rear equals front.
Queue is empty when front and rear are -1.
Reset pointers after last dequeue.
Full Transcript
This visual trace shows how a circular queue works using an array. We start with front and rear pointers at -1, meaning empty. When we enqueue, rear moves forward and front is set if queue was empty. When rear reaches the array end, it wraps to 0 to reuse space. Dequeue moves front forward similarly. If front passes rear, queue becomes empty and pointers reset. The queue is full when the next rear position equals front, so enqueue is rejected. This trace shows step-by-step pointer changes, queue array state, and visual representation of the queue after each operation.

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 automatically sorts the elements in the queue.
B. It efficiently uses all available space by wrapping around the array.
C. It allows storing different data types 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 free space 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 allows mixed 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 pointers?
easy
A. if (rear === size - 1)
B. if (rear === front)
C. if ((rear + 1) % size === front)
D. if (front === -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: 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.
  3. Final Answer:

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

    Full condition = (rear + 1) % size === front [OK]
Hint: Use modulo to wrap rear and compare with front [OK]
Common Mistakes:
  • Checking rear === front for full (actually empty condition)
  • Ignoring modulo wrap-around
  • Using front === -1 to check full
3. Consider the following JavaScript code for a circular queue of size 5. What will be the output after these operations?
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);
medium
A. [10, 20, 30, 40, 50]
B. [10, 20, 30, 40, undefined]
C. [10, 20, 30, 40, null]
D. [undefined, 10, 20, 30, 40]

Solution

  1. 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.
  2. Step 2: Verify output

    5 enqueues succeed for size 5. console.log(queue) shows [10, 20, 30, 40, 50].
  3. Final Answer:

    [10, 20, 30, 40, 50] -> Option A
  4. Quick Check:

    Full after exactly size enqueues [OK]
Hint: Check modulo full condition carefully to trace enqueue [OK]
Common Mistakes:
  • Thinking queue full after 4 enqueues (5th fails)
  • Confusing undefined and null values
  • Ignoring front and rear initial values
4. The following code snippet for dequeue operation in a circular queue has a bug. What is the bug?
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;
}
medium
A. front should be updated using modulo to wrap around
B. rear should be incremented instead of front
C. The empty check should be rear === -1
D. The function should not return null when empty

Solution

  1. Step 1: Analyze front update in dequeue

    When removing an element, front moves forward. Since queue is circular, front must wrap using modulo size.
  2. Step 2: Identify bug in front update

    Code uses front = front + 1 without modulo, so front can go beyond array size causing errors.
  3. Final Answer:

    front should be updated using modulo to wrap around -> Option A
  4. Quick Check:

    Update front with modulo to avoid overflow [OK]
Hint: Always use modulo when moving front or rear in circular queue [OK]
Common Mistakes:
  • Incrementing rear instead of front on dequeue
  • Checking empty condition incorrectly
  • Ignoring modulo wrap-around
5. You have a circular queue of size 4 implemented using an array. Initially, front = -1 and rear = -1. You perform these operations:
enqueue(1); enqueue(2); dequeue(); enqueue(3); enqueue(4); dequeue(); enqueue(5);

What is the final state of the queue array?
hard
A. [undefined, undefined, 3, 5]
B. [5, 2, 3, 4]
C. [undefined, 2, 3, 5]
D. [5, undefined, 3, 4]

Solution

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

    [5, undefined, 3, 4] -> Option D
  4. Quick Check:

    Wrap-around enqueue updates index 0 after dequeues [OK]
Hint: Track front and rear carefully with modulo wrap [OK]
Common Mistakes:
  • Not wrapping rear index on enqueue
  • Assuming dequeue clears array slots to undefined
  • Confusing front and rear after dequeues
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