Browse curriculum

Queue

A queue is a linear data structure that follows the FIFO (First In, First Out) principle.

0 / 10 · FIFO

READY

Queue is empty

Enqueue a value to begin. A queue is added to at the rear and taken from the front.

Value

Intuition

Imagine standing in line at a movie theater. The first person to arrive is the first one to buy a ticket and leave the line. New people join at the back. This is FIFO (First In, First Out).

Concept

A Queue is a linear data structure that follows the FIFO principle. Elements are added at one end (Rear) and removed from the other end (Front).

How it Works

We maintain two pointers: Front (for removal) and Rear (for insertion).

  • Enqueue: Add element to Rear, increment Rear index.
  • Dequeue: Remove element from Front, increment Front index.
  • Peek: View element at Front.

Step-by-Step Breakdown

  1. Start Empty: Front = 0, Rear = -1.
  2. Enqueue(10): Rear becomes 0, Queue[0] = 10.
  3. Enqueue(20): Rear becomes 1, Queue[1] = 20.
  4. Dequeue(): Return Queue[0] (10), Front becomes 1.

Example trace

Enqueue(A)

Person A joins the line.

[A]

Enqueue(B)

Person B joins behind A.

[A, B]

Dequeue()

Person A leaves from the front — not the one added most recently. B is the front now.

[B]

When to Use

  • Resource Sharing: Printer spooling, CPU task scheduling.
  • Asynchronous Data: Handling data buffers (IO, pipes).
  • BFS: Breadth-First Search in graphs.

When NOT to Use

  • Random Access: Need to access elements in the middle? Use Array.
  • Priority: If some items need to jump the line, use Priority Queue.

How to Identify

Look for problems involving "First come, first served", "Fairness", or "Level-order" processing. Keywords: "Buffer", "Stream", "Order preservation".

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description

Frequently Asked Questions

What is FIFO in a Queue?

FIFO stands for First-In-First-Out, meaning the first element added to the queue is the first one to be removed.


What are the main operations in a Queue?

The primary operations are Enqueue (adding to the rear) and Dequeue (removing from the front).


Real-world examples of a Queue?

Queues are used in printer job scheduling, ticket booking systems, and request handling in web servers.