🌇DSA
← Back
Data StructureBeginner
🚦

Queues

First In First Out structure

⏱️ 3–4 hours🧩 10 LeetCode
🔒 Login to unlock solutions and track progress. Login

What is it?

A queue works like a line at a cinema — the first person to join is the first to be served. This is called FIFO (First In, First Out).

  • Enqueue → join at the back of the line
  • Dequeue → leave from the front
  • Peek → see who's at the front without removing them

Variants / Subtypes

  • Simple Queue — basic FIFO
  • Circular Queue — wraps around to reuse space
  • Deque — add or remove from both ends
  • Priority Queue — highest-priority item exits first

Real-World Examples

Printer job queue
Call centre waitlist
OS task scheduler
BFS graph traversal
Food order queue
Customer support tickets

Code Example

What this code shows

Join the queue with A, B, C in that order. Dequeue removes A from the front (first in, first out). B is now at the front. Queue holds [B, C].

const queue = [];

queue.push("A"); // join back: [A]
queue.push("B"); // join back: [A, B]
queue.push("C"); // join back: [A, B, C]

// shift() removes from the FRONT (index 0)
console.log("Dequeued:", queue.shift()); // A — first in, first out
console.log("Front:   ", queue[0]);      // B — new front
console.log("Queue:   ", queue);         // [B, C]

Expected Output

Dequeued: A
Front:    B
Queue:    [B, C]

LeetCode Practice