Oh My Algorithm
Algorithm Guidecomplexity: O(1) enqueue/dequeue

Queue

A first-in-first-out (FIFO) structure where the first item in comes out first. You add at the rear and remove at the front, and it's the basis of BFS, task queues, and buffering.

01Queue

Starting the queue. A first-in-first-out (FIFO) structure where you enqueue at the rear and dequeue at the front.

enqueue(10) · Add 10 at the rear.

enqueue(24) · Add 24 at the rear.

enqueue(37) · Add 37 at the rear.

dequeue() · Mark the frontmost 10 for removal.

10 leaves and 24 becomes the new front. The first value in comes out first.

enqueue(55) · No gap opens up — 55 is added on at the rear.

dequeue() · The front 24 leaves. Values exit in the order they entered — that's FIFO.

empty
1 / 8

02 Understand It Simply

For Everyone
🔑How It Works

Values go in at one end and come out at the other, so the first one in is the first one out.

💡In Plain Words

Items enter at the rear and leave at the front.

The first item in comes out first (first-in-first-out, FIFO).

📍Where It's Used
  • Printer queues
  • call-center waiting order
  • task processing order

03 Python Implementation

A clean, readable reference implementation of the core logic of Queue.

core_implementation.py
from collections import deque

class Queue:
    def __init__(self):
        self.items = deque()

    def enqueue(self, x):
        self.items.append(x)

    def dequeue(self):
        if not self.items:
            raise IndexError("queue is empty")
        return self.items.popleft()

    def peek(self):
        return self.items[0] if self.items else None

04 Frequently Asked Questions

FAQ
What is Queue?+

A first-in-first-out (FIFO) structure where the first item in comes out first. You add at the rear and remove at the front, and it's the basis of BFS, task queues, and buffering.

What is the time complexity of Queue?+

The time complexity of Queue is O(1) enqueue/dequeue. Follow the step-by-step visualization to see exactly why.

Where is Queue used?+

Printer queues, call-center waiting order, task processing order.

What's a simple analogy for Queue?+

Values go in at one end and come out at the other, so the first one in is the first one out.