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.
Specifically, how fast can we add or remove items in a queue implemented with an array?
Analyze the time complexity of the following code snippet.
#define MAX 100
int queue[MAX];
int front = -1, rear = -1;
void enqueue(int x) {
if (rear == MAX - 1) return; // queue full
if (front == -1) front = 0;
queue[++rear] = x;
}
int dequeue() {
if (front == -1 || front > rear) return -1; // queue empty
return queue[front++];
}
This code adds (enqueue) and removes (dequeue) elements from a queue using a fixed-size array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single-step insert or remove at array index.
- How many times: Each enqueue or dequeue does one operation regardless of queue size.
Each operation takes the same small number of steps no matter how many items are in the queue.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation per enqueue or dequeue |
| 100 | 1 operation per enqueue or dequeue |
| 1000 | 1 operation per enqueue or dequeue |
Pattern observation: The time does not increase with more elements; it stays constant.
Time Complexity: O(1)
This means adding or removing an item takes the same short time no matter how big the queue is.
[X] Wrong: "Removing an item from the queue takes longer as the queue grows because we have to shift elements."
[OK] Correct: In this implementation, we do not shift elements; we just move the front index forward, so removal stays fast.
Understanding how queue operations stay fast helps you explain efficient data handling in real systems.
"What if we changed the queue to shift all elements left on dequeue? How would the time complexity change?"
Practice
Solution
Step 1: Understand queue behavior
A queue is a data structure where the first element added is the first one to be removed, which is FIFO.Step 2: Compare with options
It follows First-In-First-Out (FIFO) order correctly states FIFO order. It follows Last-In-First-Out (LIFO) order is LIFO (stack), C allows random access (array), D stores sorted (not queue).Final Answer:
It follows First-In-First-Out (FIFO) order -> Option AQuick Check:
Queue = FIFO [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[size], e.g., int queue[5];Step 2: Check each option
int queue[5]; matches correct syntax. queue int[5]; is invalid syntax. int queue(); declares a function, not an array. int queue = new int[5]; is Java-style syntax.Final Answer:
int queue[5]; -> Option BQuick Check:
C array declaration = int name[size] [OK]
- Using Java or other language syntax
- Confusing function declaration with array
- Missing semicolon or brackets
int queue[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
Initially front = -1, rear = -1. Enqueue 10: rear=0, queue[0]=10, front=0. Enqueue 20: rear=1, queue[1]=20. Dequeue: val=queue[front]=queue[0]=10, front=1. Enqueue 30: rear=2, queue[2]=30.Step 2: Identify current queue elements
front=1, rear=2, so elements are queue[1]=20 and queue[2]=30. Hence queue is 20 -> 30 -> null.Final Answer:
20 -> 30 -> null -> Option CQuick Check:
After dequeue, front moves forward; queue = 20, 30 [OK]
- Not updating front after dequeue
- Including dequeued elements in output
- Confusing front and rear indices
void enqueue(int queue[], int *rear, int size, int value) {
if (*rear == size) {
printf("Queue is full\n");
return;
}
(*rear)++;
queue[*rear] = value;
}Solution
Step 1: Analyze the full condition
The check if(*rear == size) is wrong. Array indices are 0 to size-1, so full when *rear == size-1 before incrementing. Current check allows overflow.Step 2: Compare options
rear should be compared with size - 1, not size identifies the correct fix: compare with size-1, not size. Others are incorrect.Final Answer:
rear should be compared with size - 1, not size -> Option AQuick Check:
Full: *rear == size-1 [OK]
- Comparing rear == size instead of size-1
- Incorrect overflow condition
- Ignoring zero-based indexing
Solution
Step 1: Understand initial state and operations
Initial: front=1, rear=3, size=4. Dequeue twice: front moves forward twice. Since circular, front = (1 + 2) % 4 = 3.Step 2: Enqueue one element
rear moves forward by one circularly: rear = (3 + 1) % 4 = 0.Final Answer:
front = 3, rear = 0 -> Option DQuick Check:
Circular queue uses modulo for front and rear [OK]
- Not using modulo for circular increment
- Updating front or rear incorrectly
- Confusing front and rear positions
Ready to prove it?
Pass the test and this pattern moves unknown
