Skip to main content
Back to the DSA atlas
Linear structures Data structure Intro

Queue

Process work in first-in, first-out order. Queues are the missing linear-structure foundation behind BFS, buffering, and fair scheduling.

queuefifobfslinear

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

Step 1 / 6

front
empty queue
back

Start empty

A queue processes items in first-in, first-out order.

Why queues matter

Queues model fair processing. The oldest item gets handled first, which is exactly why BFS discovers nodes level by level, schedulers process jobs in arrival order, and streaming systems buffer work without reordering it.

Family

Linear structures

State that flows from one end to the other: lists, deques, stacks, and windows.

Builds on

Foundational

You can start here without another page first.

Unlocks

3 next topics

Use these follow-ups when you want to apply the current idea in a richer setting.

Learning paths

4

This topic appears in curated progressions so you can keep moving without guessing the next step.

Problem

You need to process items in the same order they arrive.

Examples:

  • users waiting in line
  • tasks waiting for a worker
  • nodes waiting for breadth-first exploration
  • packets waiting inside a buffer

A stack is wrong because it reverses the order. A queue keeps the oldest item at the front.

Core idea

A queue supports two main operations:

  • enqueue(x) inserts at the back
  • dequeue() removes from the front

That makes it a FIFO structure: first in, first out.

Why it matters

Queues are simple, but they unlock a surprising amount of the atlas:

  • BFS depends on a queue to expand the graph one layer at a time
  • Deque generalizes a queue by allowing work at both ends
  • Monotonic queue builds on queue order plus a dominance rule

Without a queue, breadth-first processing has no clean data structure behind it.

Implementations

Array with head pointer

Fast and compact if you avoid shifting all elements on every pop.

Circular buffer

The classic fixed-capacity queue. Head and tail wrap around.

Linked list

Useful when dynamic growth matters and you want O(1)O(1) insertion/removal at both logical ends.

Complexity

OperationTime
EnqueueO(1)O(1)
DequeueO(1)O(1)
FrontO(1)O(1)

Common mistakes

  • Using an array and calling a full left-shift on every dequeue
  • Reaching for a deque when a plain queue would be clearer
  • Forgetting that FIFO order is the reason BFS gives shortest paths in unweighted graphs

Key takeaways

  • A queue is the canonical FIFO structure
  • It is the missing conceptual step between linked lists and graph traversal
  • BFS is not just β€œa graph algorithm”; it is graph traversal powered by queue discipline
  • Deque and monotonic queue both make more sense once the plain queue comes first

Practice problems

ProblemDifficultyKey idea
Number of Recent Calls🟒 EasyKeep only active items in FIFO order
Implement Stack using Queues🟑 MediumUnderstand exactly what FIFO gives you
Open the Lock🟑 MediumBFS frontier is literally a queue

Relation to other topics

  • Deque extends a queue to both ends
  • BFS uses a queue to visit nodes by distance layer
  • Monotonic queue preserves FIFO order while deleting dominated candidates

What this enables

Once the current idea feels natural, these are the most useful next jumps.

Related directions

These topics live nearby conceptually, even if they are not direct prerequisites.

More from Linear structures

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

From the blog

Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.