Chapter 15: Stacks & Queues

DSA Stacks & Queues – Let’s learn these two very important linear data structures like I’m sitting next to you, explaining slowly and clearly with lots of real-life examples, memory visuals, code, common operations, interview patterns, and honest comparison.

1. What is a Stack?

A Stack is a linear data structure that follows LIFO (Last In, First Out) principle.

That means:

  • The last element added is the first one to be removed
  • It’s exactly like a stack of plates in your kitchen

Real-life examples of Stack

Real-life situation Stack operation
Browser Back button Last page visited is on top
Undo (Ctrl + Z) in any editor Last action done is undone first
Call stack in programming Last function called returns first
Pile of books on a table You take/remove from the top
Restaurant plate stacking Wash the top plate first
Recursion Deepest call finishes first
Expression evaluation (postfix) Operands wait for operators

Core Operations of Stack (very important)

Operation Name Time Complexity What it does
Push Insert O(1) Add element to the top
Pop Remove O(1) Remove & return top element
Peek / Top View O(1) See top element without removing
isEmpty Check O(1) Check if stack is empty
Size Length O(1) Number of elements

Stack Implementation – Two most common ways

1. Using Array (most common in interviews)

Python

Why array is popular for stack:

  • Very fast (all operations O(1) amortized)
  • Cache-friendly
  • Easy to implement
  • Most companies accept this implementation

2. Using Linked List (sometimes asked)

Python

Advantage of linked list stack: No size limit (array has to resize sometimes)

Disadvantage: Slightly slower due to pointer chasing

2. What is a Queue?

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

Like a normal queue/line in real life:

  • First person who came is served first
  • New person joins at the back (rear)
  • Person leaves from the front

Real-life examples of Queue

Real-life situation Queue operation
People waiting at ticket counter First person gets ticket first
Printer job queue First document printed first
CPU process scheduling (FCFS) First process gets CPU first
Breadth-First Search (BFS) Level by level exploration
Customer support call center First caller is answered first
Buffer in streaming/video playback Oldest data played first

Core Operations of Queue

Operation Name Time Complexity (array) Time Complexity (linked list)
Enqueue Add O(1) amortized O(1)
Dequeue Remove O(n) (bad) / O(1) with circular O(1)
Front / Peek View front O(1) O(1)
isEmpty Check O(1) O(1)
Size Length O(1) O(1)

Queue Implementation – Most common ways

1. Using Array + Circular Queue (best for interviews)

Python

Important: Normal array queue has O(n) dequeue (shift elements) → We use circular queue to make both enqueue & dequeue O(1)

2. Using Python list (simple but not efficient for large use)

Python

Not recommended for serious use — use collections.deque instead

3. Best in Python – collections.deque (double-ended queue)

Python

This is the most efficient queue implementation in Python.

Quick Comparison – Stack vs Queue

Feature Stack Queue
Principle LIFO FIFO
Real-life example Pile of plates Ticket line
Main operations push, pop, peek enqueue, dequeue, front
Insert position Top Rear
Remove position Top Front
Implementation Array / Linked List Circular Array / Linked List / deque
Most famous algorithm DFS, recursion, undo BFS, level order traversal
Cache performance Excellent (array) Good (circular array)

Very Common Interview Patterns

Stack patterns

  • Valid Parentheses / balanced brackets
  • Next Greater Element
  • Largest Rectangle in Histogram
  • Stock Span Problem
  • Min Stack / design stack with O(1) min
  • Evaluate Reverse Polish Notation

Queue patterns

  • Level Order Traversal (Binary Tree)
  • BFS (Graph / Tree)
  • Sliding Window Maximum
  • Generate Binary Numbers from 1 to n
  • Rotten Oranges / Flood Fill
  • Implement Stack using Queue (and vice versa)

Would you like to go deeper into any particular topic next?

Popular choices:

  • Implement Stack using Queue (classic interview question)
  • Implement Queue using Stack
  • Next Greater Element (very common stack problem)
  • Level order traversal of binary tree (queue)
  • Valid Parentheses (stack)
  • Circular Queue dry run with memory diagram

Just tell me which one you want — I’ll explain it in the same detailed, human-teacher style! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *