Java Notes
Complete guide to Queue data structure — FIFO principle, circular queue, priority queue, deque, BFS applications, and classic problems.
What is a Queue?
A Queue is a FIFO (First In, First Out) data structure — like a line at a store. The first person who joins the line is the first person to be served. In programming, the first element added to the queue is the first one to be removed.
This behavior contrasts with a Stack (LIFO — Last In, First Out) where the most recent element is removed first. Understanding when to use a Queue vs a Stack is a fundamental skill for solving algorithmic problems.
| enqueue(4): [1, 2, 3] | [1, 2, 3, 4] (add to rear) |
| dequeue(): [1, 2, 3, 4] | [2, 3, 4] (remove from front, returns 1) |
| peek(): [2, 3, 4] | returns 2 (view front without removing) |
| isEmpty() | returns false |
| size() | returns 3 |
| All basic operations | O(1) time complexity |
Array-based Circular Queue Implementation
Understanding how to implement a queue from scratch is essential for interviews:
Why circular? Without the circular (modulo) logic, a linear array queue wastes space. As elements are dequeued, the front index advances leaving unused space behind it. The circular approach reuses that space by wrapping the rear pointer back to the beginning when it reaches the end of the array.
Priority Queue (Min-Heap)
A Priority Queue processes elements by priority rather than FIFO order. Java's PriorityQueue implements a min-heap by default:
Time complexity: offer() and poll() are O(log n). peek() is O(1).
Deque (Double-Ended Queue)
A Deque allows insertion and removal from BOTH ends:
BFS (Breadth-First Search) Using Queue
BFS is the most important algorithmic application of queues — it explores nodes level by level:
Queue Implementations Comparison
| Implementation | Use Case | Thread-Safe? | Null Elements? |
|---|---|---|---|
| ArrayDeque | General purpose (fastest) | No | No |
| LinkedList | Need null elements or List interface | No | Yes |
| PriorityQueue | Priority-based processing | No | No |
| ConcurrentLinkedQueue | Multi-threaded producers/consumers | Yes | No |
| LinkedBlockingQueue | Producer-consumer with blocking | Yes | No |
| ArrayBlockingQueue | Bounded blocking queue | Yes | No |
Interview Questions
Q1: How do you implement a queue using two stacks?
Answer: Stack1 for enqueue (push directly). Stack2 for dequeue: if Stack2 is empty, transfer ALL elements from Stack1 to Stack2 (this reverses the order), then pop from Stack2. Amortized O(1) per operation because each element is moved at most twice across its lifetime.
Q2: What is the difference between Queue, Deque, and PriorityQueue?
Answer: Queue enforces FIFO order. Deque allows insertion and removal at both ends (can act as both queue and stack). PriorityQueue orders elements by natural ordering or a comparator — it is NOT FIFO, elements come out in priority order regardless of insertion time.
Q3: What is a circular queue and why use it?
Answer: A circular queue uses modulo arithmetic to wrap pointers around the array, reusing space freed by dequeue operations. Without circularity, a linear array wastes all space before the front pointer. Circular queues provide O(1) operations with fixed memory.
Q4: How do you implement a sliding window maximum using a deque?
Answer: Maintain a monotonic decreasing deque storing indices. For each new element: remove indices outside the window from front, remove smaller elements from back (they can never be the maximum), add current index to back. The front of deque always holds the current window's maximum.
Q5: Where are queues used in real systems?
Answer: Task scheduling (CPU round-robin), print spooler (FIFO print jobs), BFS traversal (graph algorithms), message queues (Kafka, RabbitMQ), HTTP request handling (web server request queue), I/O buffering (disk and network buffers), thread pools (work queue for worker threads).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Queue in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, dsa, queue, queue in java
Related Java Master Course Topics