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.
class Queue {
int arr[100];
int front = 0, rear = 0;
public:
void enqueue(int x) {
arr[rear++] = x;
}
int dequeue() {
return arr[front++];
}
};
This code adds elements at the rear and removes from the front of the array, simulating a queue.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding or removing one element by moving front or rear index.
- How many times: Each enqueue or dequeue moves the index once, no loops involved.
Each operation does a fixed number of steps regardless of queue size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 per enqueue or dequeue |
| 100 | 1 per enqueue or dequeue |
| 1000 | 1 per enqueue or dequeue |
Pattern observation: Time per operation 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 element from the front takes time proportional to the queue size because we have to shift all elements."
[OK] Correct: In this implementation, we just move the front index forward without shifting elements, so removal is quick and constant time.
Understanding how queue operations work efficiently helps you explain data structure choices clearly and shows you know how to keep operations fast.
"What if we used a circular array to reuse space after dequeues? How would the time complexity change?"
Practice
Solution
Step 1: Understand queue behavior
A queue follows FIFO (First In First Out), meaning elements are processed in the order they arrive.Step 2: Role of array in queue
An array stores elements in a fixed linear order, which helps maintain the FIFO order by using front and rear indices.Final Answer:
To store elements in a fixed-size linear order for FIFO operations -> Option AQuick Check:
Queue uses array for FIFO storage = C [OK]
- Confusing queue with stack (LIFO)
- Thinking array sorts elements automatically
- Assuming random access is main goal
Solution
Step 1: Recall C++ array declaration syntax
In C++, arrays are declared as: type name[size]; so int queue[5]; is correct.Step 2: Check other options
Options A, B, and C do not follow valid C++ syntax for array declaration.Final Answer:
int queue[5]; -> Option BQuick Check:
C++ array syntax = type name[size] [OK]
- Placing size before name
- Using wrong keywords like 'array'
- Incorrect order of type and name
enqueue(10); enqueue(20); dequeue(); enqueue(30); display();Assume enqueue adds at rear and dequeue removes from front.
Solution
Step 1: Perform enqueue(10) and enqueue(20)
Queue becomes: 10 -> 20 -> nullStep 2: Perform dequeue()
Removes 10 from front, queue becomes: 20 -> nullStep 3: Perform enqueue(30)
Add 30 at rear, queue becomes: 20 -> 30 -> nullStep 4: Display queue
Output is: 20 -> 30 -> nullFinal Answer:
20 -> 30 -> null -> Option DQuick Check:
After dequeue and enqueue, queue = 20, 30 [OK]
- Not removing element on dequeue
- Adding new element at front instead of rear
- Confusing order of elements
int queue[3], front = -1, rear = -1;
void enqueue(int x) {
if (rear == 3) {
cout << "Queue Full";
return;
}
rear++;
queue[rear] = x;
if (front == -1) front = 0;
}Solution
Step 1: Understand array indexing for size 3
Array indices are 0, 1, 2. So rear should not exceed 2.Step 2: Check condition for full queue
Condition rear == 3 is wrong because index 3 is out of bounds. It should be rear == 2.Final Answer:
Condition should be rear == 2 to check full queue -> Option CQuick Check:
Array max index = size-1 = 2 [OK]
- Using size instead of size-1 for index check
- Incrementing front in enqueue
- Initializing rear incorrectly
enqueue(5); enqueue(10); dequeue(); enqueue(15); enqueue(20); dequeue(); enqueue(25); display();Assume no circular queue and enqueue fails if rear reaches size-1.
Solution
Step 1: Perform enqueue(5), enqueue(10)
Queue: 5 -> 10 -> null; front=0, rear=1Step 2: Perform dequeue()
Remove 5; Queue: 10 -> null; front=1, rear=1Step 3: Perform enqueue(15), enqueue(20)
Queue: 10 -> 15 -> 20 -> null; front=1, rear=3Step 4: Perform dequeue()
Remove 10; Queue: 15 -> 20 -> null; front=2, rear=3Step 5: Perform enqueue(25)
rear=3 (max index), cannot enqueue 25; operation fails, queue unchangedStep 6: Display queue
Queue elements from front=2 to rear=3: 15 -> 20 -> nullFinal Answer:
15 -> 20 -> null -> Option AQuick Check:
Check enqueue failure at full rear index [OK]
- Assuming enqueue works after rear reaches max
- Not updating front after dequeue
- Confusing queue order after operations
Ready to prove it?
Pass the test and this pattern moves unknown
