Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
DSAQueuesData StructuresFIFO

DSA Queues — FIFO Fundamentals Visualized

Learn queue fundamentals — FIFO enqueue/dequeue, O(1) operations, interactive step visualizer, code breakdown, and real-world scheduling examples.

Jul 4, 20263 min read

Introduction

Queues are FIFO (First In, First Out) structures — the first element enqueued is the first one dequeued. Think of a line at a coffee shop: first person in line gets served first.

Queues power BFS, task schedulers, print spoolers, and message brokers like Kafka.

Quick index

#Section
1How queues work
2Interactive visualizer
3Full implementation
4Complexity & use cases
5Real-life examples

1. How queues work

plaintext
front → [10] [20] [30] ← rear
         ↑              ↑
      dequeue        enqueue
OperationActionResult
enqueueAdd at rearO(1)
dequeueRemove from frontO(1)*
frontRead first, no removeO(1)
isEmptyCheck if emptyO(1)

Note

JavaScript Array.shift() is O(n) due to re-indexing. Production queues use a linked list or circular buffer for true O(1) dequeue.

Back to index


2. Interactive visualizer

Queue — Fundamentals

FIFO structure — enqueue at rear, dequeue from front. Foundation for BFS, task schedulers, and message brokers.

front→QUEUE→rear
empty

Queue starts empty — enqueue at rear, dequeue from front.

Queue class

class Queue {
  constructor() { this.items = [] }
  enqueue(v) { this.items.push(v) }
  dequeue() { return this.items.shift() }
  front() { return this.items[0] }
  isEmpty() { return this.items.length === 0 }
}

FIFO — First In, First Out.

enqueue

O(1)

dequeue

O(1)*

Order

FIFO

Back to index


3. Full implementation

js
class Queue {
  constructor() {
    this.items = []
  }
  enqueue(val) {
    this.items.push(val)
  }
  dequeue() {
    return this.items.shift()
  }
  front() {
    return this.items[0]
  }
  isEmpty() {
    return this.items.length === 0
  }
}

Function calls with output

js
// ── Demo calls — output shown on the right ──
const q = new Queue()
 
q.enqueue(10) // → [10]
q.enqueue(20) // → [10, 20]
q.enqueue(30) // → [10, 20, 30]
q.front() // → 10
q.dequeue() // → 10
q.dequeue() // → 20
q.isEmpty() // → false

Back to index


4. Complexity & use cases

Use caseWhy queue
BFSProcess nodes level-by-level
Print spoolerFirst submitted doc prints first
Task schedulerFair FIFO job ordering
Message brokerOrdered event processing

Interview Answer

"Stack vs queue for graph traversal?"

BFS uses a queue (level order). DFS uses a stack (depth first). Same graph, different exploration order.

Back to index


5. Real-life examples

ScenarioQueue role
Support ticketsFirst submitted ticket answered first
Print queueDocuments print in submission order
BFS shortest pathExplore neighbors before going deeper
Kafka partitionsConsumers process messages in order

Customer support ticket queue

First submitted ticket gets answered first — classic FIFO fairness.

T-101Billing[0]
T-102Login[1]

Customers submit — enqueue at rear.

Real-world implementation

class TicketQueue {
  constructor() { this.tickets = [] }
  submit(ticket) { this.tickets.push(ticket); return this.tickets.length }
  assignNext() { return this.tickets.shift() ?? null }
  peekNext() { return this.tickets[0] ?? null }
  waitingCount() { return this.tickets.length }
}

Full working code for the scenario above.

Function calls with output

// ── Function calls with output ──
const q = new TicketQueue()
q.submit({ id:"T-101", issue:"Billing" })→ position 1
q.submit({ id:"T-102", issue:"Login" })→ position 2

Side output shows return value after each call — step through to trace execution.

Back to index


Summary

Queues give FIFO ordering — essential whenever fairness or level-order processing matters.

Next reads: Queue Types · Queue Applications · DSA Stacks

Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime