PadhaikoAll subjects

Past exam question: 2073 Bhadra (Regular), Q2(a)

Data Structure and Algorithms · 5 marks

Open in the Padhaiko app

Topic: Queues

Question

Explain how a circular queue differ from linear queue with suitable example.

Solution

Key Differences (Circular Queue vs. Linear Queue):

  1. Pointer Wrap-Around: A linear queue increments rear and front linearly (+1); a circular queue wraps them modulo MAX ((idx + 1) % MAX).
  2. Memory Reuse & Overflow: A linear queue triggers false overflow once rear == MAX - 1 even if slots 0..front-1 were freed by dequeue(). A circular queue reuses every freed slot until all MAX slots (or MAX - 1 in sentinel indexing) are full.

Example (MAX = 4): Insert 10, 20, 30, 40 (front=0, rear=3), then dequeue 10, 20 (front=2, rear=3).

Answer:

A circular queue wraps front and rear with (index + 1) % MAX so freed front slots are immediately reused, avoiding the false overflow of a linear queue when rear == MAX - 1.

All past exam questions for Data Structure and Algorithms