MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Queues: FIFO Mechanics, Circular Buffers & Double-Ended Queues (Deque)0%
Study Materials
Data Structures & AlgorithmsProgress 0%
1.1Introduction to DSA, Big-O, Big-Omega & Big-Theta Notations1.2Time Complexity Calculation Rules & Common Runtime Patterns1.3Space Complexity, Auxiliary Memory & Stack vs Heap Allocation
4.1Stacks: LIFO Operations, Array/List Implementations & Bracket Matching4.2Monotonic Stacks: Next Greater Element & Histogram Area Patterns4.3Queues: FIFO Mechanics, Circular Buffers & Double-Ended Queues (Deque)4.4Hash Tables: Hash Functions, Separate Chaining & Open Addressing4.5Hash Map Patterns: Two Sum, Frequency Counters & Subarray Sums
HomeTutorialsData Structures & AlgorithmsQueues: FIFO Mechanics, Circular Buffers & Double-Ended Queues (Deque)
Hash Tables: Hash Functions, Separate Chaining & Open Addressing

Queues: FIFO Mechanics, Circular Buffers & Double-Ended Queues (Deque)

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

A Queue is a linear data structure governed by the FIFO (First In, First Out) principle: the first element added to the queue is the first one removed. Queues are indispensable for asynchronous task dispatching, printer spoolers, network packet routing, and Breadth-First Search (BFS) graph traversals.


1. Real-World Analogy: The Coffee Shop Counter

Think of ordering coffee at a drive-through or counter:

  • The first customer to arrive in line is the first one served and leaves with their drink (FIFO).
  • New customers join at the back (Rear / Enqueue).
  • Customers leave from the front (Front / Dequeue).
  • Cutting in line is forbidden!

2. Queue Operations & Mechanics

OperationActionTime Complexity
enqueue(val)Appends an element to the rear of the queue$O(1)$
dequeue()Removes and returns the front element$O(1)$
peek() / front()Inspects the front element without removing it$O(1)$
isEmpty()Checks if the queue is empty$O(1)$

3. Why Array .shift() is an Anti-Pattern in JavaScript/Python

In languages like JavaScript, calling arr.shift() to dequeue an element removes the first element and shifts every remaining element in memory 1 index to the left:

1// BAD ANTI-PATTERN: Takes O(n) per dequeue!
2const queue = [];
3queue.push(10); // Enqueue: O(1)
4queue.shift(); // Dequeue: O(n) due to memory shifting!

For 100,000 operations, using shift() takes seconds and triggers Time Limit Exceeded (TLE) in interviews.

The Solutions:

  1. 1
    Circular Array (Ring Buffer): Uses modulo arithmetic (tail + 1) % capacity to wrap around without shifting.
  2. 2
    Double-Ended Queue (Deque): Implemented via doubly linked nodes (Python collections.deque).

4. Multi-Language Implementations: Circular Queue

Python 3 Implementation:

1class MyCircularQueue:
2 def __init__(self, k: int):
3 self.capacity = k
4 self.queue = [0] * k
5 self.head = 0
6 self.tail = 0
7 self.size = 0
8 
9 def en_queue(self, value: int) -> bool:
10 if self.is_full():
11 return False
12 self.queue[self.tail] = value
13 # Modulo arithmetic wraps tail back to 0 when reaching capacity!
14 self.tail = (self.tail + 1) % self.capacity
15 self.size += 1
16 return True
17 
18 def de_queue(self) -> bool:
19 if self.is_empty():
20 return False
21 # Advance head pointer with modulo
22 self.head = (self.head + 1) % self.capacity
23 self.size -= 1
24 return True
25 
26 def Front(self) -> int:
27 return -1 if self.is_empty() else self.queue[self.head]
28 
29 def Rear(self) -> int:
30 if self.is_empty():
31 return -1
32 # Tail points to NEXT write position, so last item is at (tail - 1)
33 return self.queue[(self.tail - 1 + self.capacity) % self.capacity]
34 
35 def is_empty(self) -> bool:
36 return self.size == 0
37 
38 def is_full(self) -> bool:
39 return self.size == self.capacity

Modern JavaScript / TypeScript (High-Performance Deque via Doubly Linked Nodes):

1class DequeNode<T> {
2 val: T;
3 prev: DequeNode<T> | null = null;
4 next: DequeNode<T> | null = null;
5 constructor(val: T) {
6 this.val = val;
7 }
8}
9 
10export class Deque<T> {
11 private head: DequeNode<T> | null = null;
12 private tail: DequeNode<T> | null = null;
13 public size: number = 0;
14 
15 public pushBack(val: T): void {
16 const node = new DequeNode(val);
17 if (!this.tail) {
18 this.head = this.tail = node;
19 } else {
20 node.prev = this.tail;
21 this.tail.next = node;
22 this.tail = node;
23 }
24 this.size++;
25 }
26 
27 public popFront(): T | null {
28 if (!this.head) return null;
29 const val = this.head.val;
30 this.head = this.head.next;
31 if (this.head) {
32 this.head.prev = null;
33 } else {
34 this.tail = null;
35 }
36 this.size--;
37 return val;
38 }
39}

5. Trace Table: Circular Queue of Capacity 3

OperationActionArray StateheadtailsizeReturn
enQueue(10)Writes at 0[10, _, _]011True
enQueue(20)Writes at 1[10, 20, _]022True
enQueue(30)Writes at 2[10, 20, 30]00 (wrapped!)3 (Full)True
enQueue(40)Attempt write[10, 20, 30]003False (Full)
deQueue()Removes 10[_, 20, 30]102True
enQueue(40)Writes at 0[40, 20, 30]113True (Reused 0!)

Notice how index 0 was recycled seamlessly with zero element copies!


Interactive Quiz• Knowledge Check

Multiple Choice Questions

Question 1
Which data processing order governs the behavior of a standard Queue?
Question 2
Why is using standard JavaScript array.shift() to implement a queue in performance-critical code an anti-pattern?
Question 3
What mathematical operator allows a Circular Queue to wrap its pointer from the end of the array back to index 0?
Question 4
What is a "Double-Ended Queue" (Deque)?
Question 5
In which major graph algorithm is a FIFO Queue strictly required to explore nodes level-by-level?

Next Lesson

Hash Tables: Hash Functions, Separate Chaining & Open Addressing

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Monotonic Stacks: Next Greater Element & Histogram Area PatternsHash Tables: Hash Functions, Separate Chaining & Open Addressing

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Data Structures & Algorithms with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext