Queue Implementation Using Array in DSA C# - Time & Space Complexity
We want to understand how the time taken by queue operations changes as the number of elements grows.
How fast can we add or remove items from a queue implemented with an array?
Analyze the time complexity of the following queue operations using an array.
public class QueueArray {
private int[] arr;
private int front, rear, size, capacity;
public QueueArray(int capacity) {
this.capacity = capacity;
arr = new int[capacity];
front = 0;
rear = -1;
size = 0;
}
public void Enqueue(int item) {
if (size == capacity) throw new InvalidOperationException("Queue full");
rear = (rear + 1) % capacity;
arr[rear] = item;
size++;
}
public int Dequeue() {
if (size == 0) throw new InvalidOperationException("Queue empty");
int item = arr[front];
front = (front + 1) % capacity;
size--;
return item;
}
}
This code adds (Enqueue) and removes (Dequeue) items from a fixed-size array acting as a circular queue.
Look for loops or repeated steps inside the operations.
- Primary operation: Adding or removing one item involves simple index updates and assignments.
- How many times: Each Enqueue or Dequeue runs once per call, no loops inside.
Each operation does a fixed number of steps regardless of queue size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5-6 steps per operation |
| 100 | 5-6 steps per operation |
| 1000 | 5-6 steps per operation |
Pattern observation: The time to add or remove an item stays the same no matter how many items are in the queue.
Time Complexity: O(1)
This means each enqueue or dequeue operation takes the same small amount of time, no matter the queue size.
[X] Wrong: "Removing an item from the queue takes longer as the queue grows because we have to shift elements."
[OK] Correct: This queue uses a circular array with front and rear indexes, so no shifting is needed. Each operation just moves an index.
Understanding this helps you explain how queues work efficiently in real programs and shows you know how to manage data with fixed storage.
"What if we changed the array to resize dynamically when full? How would the time complexity of enqueue change then?"
Practice
Solution
Step 1: Understand queue behavior
A queue adds elements at the rear and removes from the front, following FIFO order.Step 2: Compare options with queue properties
Only It follows First-In-First-Out (FIFO) order. correctly describes FIFO behavior; others describe different data structures or incorrect behavior.Final Answer:
It follows First-In-First-Out (FIFO) order. -> Option DQuick Check:
Queue = FIFO order [OK]
- Confusing queue with stack (LIFO)
- Thinking queue allows random access
- Assuming queue sorts elements automatically
Solution
Step 1: Recall C# array declaration syntax
In C#, arrays are declared as type[] name = new type[size];Step 2: Check each option
int[] queue = new int[5]; matches correct syntax; others have syntax errors or wrong usage.Final Answer:
int[] queue = new int[5]; -> Option CQuick Check:
C# array declaration = int[] name = new int[size] [OK]
- Missing [] after type
- Using parentheses instead of brackets
- Assigning array without new keyword
int[] queue = new int[3]; int front = -1, rear = -1; // Enqueue 10 rear++; queue[rear] = 10; if(front == -1) front = 0; // Enqueue 20 rear++; queue[rear] = 20; // Dequeue int val = queue[front]; front++; // Enqueue 30 rear++; queue[rear] = 30; // Current queue elements from front to rear?
Solution
Step 1: Trace enqueue and dequeue operations
Enqueue 10: rear=0, front=0, queue=[10,_,_] Enqueue 20: rear=1, queue=[10,20,_] Dequeue: val=queue[front]=10, front=1 Enqueue 30: rear=2, queue=[10,20,30]Step 2: Identify current queue elements
Front=1, rear=2, so elements are queue[1]=20 and queue[2]=30Final Answer:
[20, 30] -> Option BQuick Check:
After dequeue front=1, queue elements = [20, 30] [OK]
- Not updating front after dequeue
- Including dequeued elements in output
- Confusing front and rear positions
void Enqueue(int[] queue, ref int rear, int value, int size) {
if (rear == size) {
Console.WriteLine("Queue is full");
return;
}
rear++;
queue[rear] = value;
}Solution
Step 1: Analyze boundary check for full queue
Array indices go from 0 to size-1, so rear == size means out of bounds.Step 2: Correct condition for full queue
Check if rear == size - 1 before enqueue to avoid overflow.Final Answer:
rear should be checked against size - 1, not size. -> Option AQuick Check:
Array max index = size - 1 [OK]
- Checking rear == size causes index out of range
- Incrementing rear before check
- Ignoring array bounds in enqueue
Enqueue(5) Enqueue(10) Enqueue(15) Dequeue() Enqueue(20) Enqueue(25)What will be the state of the queue array and the values of front and rear?
Solution
Step 1: Perform each operation with circular logic
Enqueue 5: front=0, rear=0, queue=[5,_,_,_] Enqueue 10: rear=1, queue=[5,10,_,_] Enqueue 15: rear=2, queue=[5,10,15,_] Dequeue: front=1 (removed 5) Enqueue 20: rear=3, queue=[5,10,15,20] Enqueue 25: rear wraps to 0, queue=[25,10,15,20]Step 2: Final front and rear positions
front=1, rear=0, queue array = [25,10,15,20]Final Answer:
Queue: [25, 10, 15, 20], front=1, rear=0 -> Option AQuick Check:
Circular queue wraps rear index [OK]
- Not wrapping rear index after reaching end
- Not updating front after dequeue
- Assuming linear queue behavior
Ready to prove it?
Pass the test and this pattern moves unknown
