Skip to content
Think & Reflect · Q2

Q.Can you implement a queue data structure using tuple or dictionary?

Puducherry CbseNCERTSubjective· 2mImportance★★★★★est
100% · 14/14 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Tuple → no; dictionary → yes. A tuple is immutable, so an enqueue or dequeue can only be faked by rebuilding the whole tuple (O(n) per operation, and the "queue" object changes identity every time). A dictionary can implement a queue properly: use two integer counters front and rear as keys; enqueue writes d[rear], dequeue pops d[front], both in O(1). A list remains the natural choice for the syllabus.

What a queue must support

A queue is FIFO — first in, first out. Any implementation must provide four operations:

OperationMeaning
is_empty()is the queue empty?
enqueue(x)add x at the rear
dequeue()remove and return the element at the front
peek()look at the front element without removing it

So the question is really: can a tuple / a dictionary support insertion and deletion?

Attempt 1 — a tuple

q = (10, 20, 30)
q[0] = 5          # TypeError

Output

TypeError: 'tuple' object does not support item assignment

A tuple is immutable. It has no append, no pop, no item assignment. The only way to "enqueue" is to construct a brand-new tuple:

q = ()
q = q + (10,)          # "enqueue" 10  -> a NEW tuple
q = q + (20,)          # "enqueue" 20  -> another NEW tuple
q = q + (30,)
print("Queue :", q)

front = q[0]           # "dequeue"
q = q[1:]              # again a NEW tuple
print("Dequeued :", front)
print("Queue :", q)

Output

Queue : (10, 20, 30)
Dequeued : 10
Queue : (20, 30)
Watch out

This looks like it works, but nothing was ever inserted or deleted. Every operation copied all n elements into a fresh tuple, so each enqueue/dequeue costs O(n) instead of O(1), and the name q is re-bound to a different object each time — a function could not modify the caller's queue in place. A tuple is therefore not a genuine queue implementation.

Attempt 2 — a dictionary

A dictionary is mutable, and keys can be integers. Keep two counters: front (index of the next element to leave) and rear (index where the next element will be stored).

class DictQueue:
    def __init__(self):
        self.data = {}       # the dictionary
        self.front = 0
        self.rear = 0

    def is_empty(self):
        return self.front == self.rear

    def enqueue(self, item):
        self.data[self.rear] = item
        self.rear += 1

    def dequeue(self):
        if self.is_empty():
            return "Queue is empty (underflow)"
        item = self.data.pop(self.front)
        self.front += 1
        return item

    def peek(self):
        return "Queue is empty" if self.is_empty() else self.data[self.front]


q = DictQueue()
for x in (10, 20, 30):
    q.enqueue(x)
    print("enqueue", x, "->", q.data)

print("peek     ->", q.peek())
print("dequeue  ->", q.dequeue(), "| queue now", q.data)
print("dequeue  ->", q.dequeue(), "| queue now", q.data)
print("dequeue  ->", q.dequeue(), "| queue now", q.data)
print("dequeue  ->", q.dequeue())

Output

enqueue 10 -> {0: 10}
enqueue 20 -> {0: 10, 1: 20}
enqueue 30 -> {0: 10, 1: 20, 2: 30}
peek     -> 10
dequeue  -> 10 | queue now {1: 20, 2: 30}
dequeue  -> 20 | queue now {2: 30}
dequeue  -> 30 | queue now {}
dequeue  -> Queue is empty (underflow)

FIFO order is preserved: 10 entered first and left first.

Step-by-step state

StepOperationfrontreardictionaryreturned
1enqueue(10)01{0: 10}—
2enqueue(20)02{0: 10, 1: 20}—
3enqueue(30)03{0: 10, 1: 20, 2: 30}—
4dequeue()13{1: 20, 2: 30}10
5dequeue()23{2: 30}20

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.