Queue Concept and FIFO Principle in DSA Java - Time & Space Complexity
We want to understand how the time to add or remove items from a queue changes as the queue grows.
How does the queue handle operations when it gets bigger?
Analyze the time complexity of the following code snippet.
public class SimpleQueue {
private int[] items;
private int front, rear, size, capacity;
public SimpleQueue(int capacity) {
this.capacity = capacity;
items = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
public void enqueue(int item) {
if (size == capacity) return; // queue full
rear = (rear + 1) % capacity;
items[rear] = item;
size++;
}
public int dequeue() {
if (size == 0) return -1; // queue empty
int item = items[front];
front = (front + 1) % capacity;
size--;
return item;
}
}
This code adds (enqueue) and removes (dequeue) items following the FIFO rule in a fixed-size queue.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing one item at a time.
- How many times: Each enqueue or dequeue runs once per operation, no loops inside.
Each enqueue or dequeue takes the same amount of time no matter how many items are in the queue.
| Input Size (n) | Approx. Operations per enqueue/dequeue |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even as the queue grows.
Time Complexity: O(1)
This means adding or removing an item takes a constant time regardless of queue size.
[X] Wrong: "Removing an item from the queue takes longer as the queue gets bigger because we have to shift all items."
[OK] Correct: The queue uses front and rear pointers to track positions, so no shifting is needed. Each operation just moves a pointer, keeping time constant.
Understanding that queue operations run in constant time shows you know how to use data structures efficiently, a key skill in coding interviews and real projects.
"What if the queue was implemented using a linked list instead of an array? How would the time complexity of enqueue and dequeue change?"
Practice
Solution
Step 1: Understand FIFO meaning
FIFO means the first element that enters the queue is the first to leave.Step 2: Match FIFO to queue behavior
Queues follow FIFO, so the first added element is removed first.Final Answer:
First In, First Out - the first element added is the first removed -> Option CQuick Check:
FIFO = First In, First Out [OK]
- Confusing FIFO with LIFO
- Thinking queue removes last added first
- Mixing stack behavior with queue
q?Solution
Step 1: Recall Java Queue methods
Java Queue interface usesadd()oroffer()to add elements.Step 2: Identify correct method
push()andenqueue()are not Queue methods;insert()is invalid.Final Answer:
q.add(10); -> Option AQuick Check:
Use add() to insert in Java Queue [OK]
- Using push() which is for stacks
- Using enqueue() which is not a Java Queue method
- Using insert() which does not exist in Queue
import java.util.*;
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(5);
q.add(10);
q.add(15);
System.out.print(q.poll() + " -> ");
System.out.print(q.poll() + " -> ");
System.out.print(q.poll() + " -> ");
}
}Solution
Step 1: Add elements to queue
Elements 5, 10, 15 are added in order to the queue.Step 2: Remove elements using poll()
poll() removes elements in FIFO order: 5, then 10, then 15.Final Answer:
5 -> 10 -> 15 -> -> Option BQuick Check:
Queue poll() removes in FIFO order [OK]
- Thinking poll() removes last element
- Confusing queue with stack order
- Assuming poll() does not remove element
Queue<String> q = new LinkedList<>();
q.add("apple");
q.add("banana");
String first = q.remove();
String second = q.remove();
String third = q.remove();Solution
Step 1: Check number of elements added
Only two elements "apple" and "banana" are added to the queue.Step 2: Analyze remove() calls
First two remove() calls remove "apple" and "banana". Third remove() tries to remove from empty queue causing exception.Final Answer:
Third remove() causes NoSuchElementException -> Option DQuick Check:
Removing from empty queue throws exception [OK]
- Assuming remove() returns null if empty
- Thinking queue can't store Strings
- Believing remove() method is missing
Queue<Integer> q = new LinkedList<>();
// Assume q is filled with IDs including negatives
while (!q.isEmpty()) {
int id = q.poll();
// What to do here?
}Solution
Step 1: Understand queue processing with condition
We want to skip negative IDs but keep processing others in order.Step 2: Choose correct action for negative IDs
Ignoring negative IDs and continuing preserves FIFO order and processes valid IDs only.Final Answer:
If id is negative, ignore and continue; else process it -> Option AQuick Check:
Skip negatives, process others in FIFO [OK]
- Re-adding negative IDs causing infinite loop
- Stopping queue on first negative ID
- Removing all elements on negative ID
Ready to prove it?
Pass the test and this pattern moves unknown
