Data Structures · Visualization

Circular Queue: Dequeue

Circular Queue

Removing an element from a circular queue reads the value at front, then advances front modulo the capacity, freeing that slot for future wrap-around enqueues. Time: O(1). Space: O(1) extra.

Big-O: enqueue / dequeue both O(1) via wrap-around indices on a fixed array.

Circular Queue: Dequeue

Circular Queue

Removing an element from a circular queue reads the value at front, then advances front modulo the capacity, freeing that slot for future wrap-around enqueues.

How Dequeue Works

  1. Check if the queue is empty (size === 0); if so, return an error/undefined.
  2. Read the value stored at index front.
  3. Advance front: front = (front + 1) % capacity.
  4. Decrement size by 1.

Dequeue Worked Example

Input: Capacity 4, [60,20,30,40] front=1 rear=0 size=4, dequeue

  1. size(4) !== 0, so proceed
  2. value = array[1] = 20
  3. front = (1+1) % 4 = 2
  4. size becomes 3

Output: Returned 20; queue now conceptually holds 30,40,60

Try your own input
DequeueO(1)
Reads and clears the front slot, then advances front forward (mod capacity).
60
slot 0FRONT▼
20
slot 1
30
slot 2
40
slot 3REAR▼
front = 0rear = 3size = 4 / 4no wrap
Active slot Front Rear ∅ empty / reusable
Dequeue
Press ▶ Run to watch it step by step.
Python · Circular Queues in Code
def dequeue(q, head, size, cap):
    if size == 0:
        raise RuntimeError("empty")
    v = q[head]
    head = (head + 1) % cap
    return v, head, size - 1

# head, size = 0, 2
# v, head, size = dequeue(q, head, size, 4)
# example: dequeue on [60, 20, 30, 40]
Input: Capacity 4, [60,20,30,40] front=1 rear=0 size=4, dequeue

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Dequeue Time & Space Complexity

TimeO(1)
SpaceO(1) extra

When to Use Dequeue

  • Consumer side of producer-consumer buffers
  • Processing the oldest event in a ring buffer
  • Round-robin CPU scheduling dispatch

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.