Model Question Paper (as circulated)Part B · Q11b4 marksCO2Module 1asked in 2 papers
Question 11b
Write algorithms to insert and delete elements from a double ended queue.
Topics: Double-ended queues
Watch this exact question run
Loading visualizer…
Solution
Assumptions:
- Circular array; front = rear = −1 when empty; full when (rear + 1) mod MAX = front.
algorithm
InsertFront(x): if full → overflow; if empty front = rear = 0 else front = (front − 1 + MAX) mod MAX; dq[front] = x
InsertRear(x): if full → overflow; if empty front = rear = 0 else rear = (rear + 1) mod MAX; dq[rear] = x
DeleteFront(): if empty → underflow; item = dq[front]; if front = rear → front = rear = −1 else front = (front + 1) mod MAX
DeleteRear(): if empty → underflow; item = dq[rear]; if front = rear → front = rear = −1 else rear = (rear − 1 + MAX) mod MAX| Operation | Result | front, rear | Array (index 0 … 4) | Front → rear |
|---|---|---|---|---|
| insertRear(10) | ok | 0, 0 | [10 _ _ _ _] | 10 |
| insertRear(20) | ok | 0, 1 | [10 20 _ _ _] | 10 20 |
| insertFront(5) | ok | 4, 1 | [10 20 _ _ 5] | 5 10 20 |
| insertFront(1) | ok | 3, 1 | [10 20 _ 1 5] | 1 5 10 20 |
| insertRear(30) | ok | 3, 2 | [10 20 30 1 5] | 1 5 10 20 30 |
| insertFront(0) | OVERFLOW | 3, 2 | [10 20 30 1 5] | 1 5 10 20 30 |
| deleteFront | removed 1 | 4, 2 | [10 20 30 _ 5] | 5 10 20 30 |
| deleteRear | removed 30 | 4, 1 | [10 20 _ _ 5] | 5 10 20 |
| deleteRear | removed 20 | 4, 0 | [10 _ _ _ 5] | 5 10 |
| deleteFront | removed 5 | 0, 0 | [10 _ _ _ _] | 10 |
MAX = 5. “_” marks a slot that holds no live element.
How to write it in the exam
Write the four procedures above, each with its overflow or underflow check. Mention that the index arithmetic wraps around with mod MAX, so every operation is O(1).
How this answer was verified
- tests/linear-expression.test.ts
- deque_array.c matches the engine on 200 random cases