Queue Implementation Using Array in DSA Python - Time & Space Complexity
We want to understand how the time taken by queue operations changes as the number of items grows.
How does adding or removing items affect the work done?
Analyze the time complexity of the following queue operations using an array.
class Queue:
def __init__(self, capacity):
self.queue = [None] * capacity
self.front = 0
self.rear = 0
self.size = 0
def enqueue(self, item):
if self.size == len(self.queue):
raise Exception('Queue is full')
self.queue[self.rear] = item
self.rear = (self.rear + 1) % len(self.queue)
self.size += 1
def dequeue(self):
if self.size == 0:
raise Exception('Queue is empty')
item = self.queue[self.front]
self.front = (self.front + 1) % len(self.queue)
self.size -= 1
return item
This code adds items to the rear and removes from the front using a circular array.
Look at what repeats when we add or remove items.
- Primary operation: Assigning or reading one element in the array.
- How many times: Exactly once per enqueue or dequeue call.
Each enqueue or dequeue does a fixed amount of work, 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 work stays the same per operation, regardless of queue size.
Time Complexity: O(1)
This means each add or remove action takes the same small amount of time, no matter how many items are in the queue.
[X] Wrong: "Removing an item from the front takes longer as the queue grows because we have to shift elements."
[OK] Correct: This implementation uses a circular array, so no shifting happens. The front index just moves forward, keeping removal time constant.
Understanding this helps you explain why queues can be efficient and how to implement them well, a skill often valued in coding discussions.
"What if we used a simple array without circular indexing and shifted elements on dequeue? How would the time complexity change?"
Practice
Solution
Step 1: Understand queue behavior
A queue adds elements at the rear and removes from the front, following FIFO.Step 2: Compare options
LIFO is for stacks, random access and sorting are not queue properties.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: Identify array type in Python
Python lists are used as arrays and initialized with [] for empty list.Step 2: Check other options
{} is dict, () is tuple, set() is a set, none are arrays.Final Answer:
queue = [] -> Option DQuick Check:
Empty array = [] [OK]
- Using {} which creates a dictionary
- Using () which creates a tuple
- Using set() which is unordered
queue = [] queue.append(10) queue.append(20) queue.append(30) print(queue.pop(0)) print(queue)
Solution
Step 1: Trace queue operations
Append adds 10, 20, 30 to queue: [10, 20, 30]. pop(0) removes first element 10.Step 2: Check print outputs
First print outputs 10, second print outputs remaining queue [20, 30].Final Answer:
10 [20, 30] -> Option BQuick Check:
pop(0) removes front = 10 [OK]
- Using pop() instead of pop(0) removes last element
- Confusing append order
- Expecting pop(0) to remove last element
queue = [] queue.append(1) queue.append(2) queue.pop() print(queue)
Solution
Step 1: Understand pop() behavior
pop() without index removes last element, not front as queue requires.Step 2: Check queue correctness
Queue must remove front element, so pop(0) should be used instead of pop().Final Answer:
pop() removes last element, not front -> Option AQuick Check:
Queue removal = pop(0) front [OK]
- Using pop() removes last, not front
- Confusing append usage
- Thinking queue is a dictionary
Solution
Step 1: Understand fixed-size array limits
An array of length 3 can hold only 3 elements; no extra space for 4th.Step 2: Check enqueue behavior without resizing
Without resizing or circular logic, adding 4th element causes overflow error or failure.Final Answer:
The queue overflows and cannot add the 4th element -> Option CQuick Check:
Fixed array size = no extra elements [OK]
- Assuming automatic resizing happens
- Thinking 4th element replaces first
- Believing queue removes last element automatically
Ready to prove it?
Pass the test and this pattern moves unknown
