Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
B.Tech S3 (R) (FT/WP) Examination November 2025 (2024 Scheme)QP 06PCCST303112503Part B · Q9b3 marksCO1Module 1asked in 2 papers

Question 9b

Write algorithm for various insertion operations of a double ended queue using array.

Topics: Double-ended queues

Watch this exact question run

Loading visualizer…

Solution

Assumptions:
  • Circular array dq[0 … MAX−1]; front = rear = −1 when empty.
algorithm
Algorithm InsertFront(x)
1. if (rear + 1) mod MAX = front then print "Overflow"; return
2. if front = −1 then front ← 0; rear ← 0
3. else front ← (front − 1 + MAX) mod MAX
4. dq[front] ← x

Algorithm InsertRear(x)
1. if (rear + 1) mod MAX = front then print "Overflow"; return
2. if front = −1 then front ← 0; rear ← 0
3. else rear ← (rear + 1) mod MAX
4. dq[rear] ← x
Example: MAX = 4checked by code
OperationResultfront, rearArray (index 0 … 3)Front → rear
insertRear(10)ok0, 0[10 _ _ _]10
insertFront(5)ok3, 0[10 _ _ 5]5 10
insertRear(20)ok3, 1[10 20 _ 5]5 10 20
insertFront(1)ok2, 1[10 20 1 5]1 5 10 20
insertRear(30)OVERFLOW2, 1[10 20 1 5]1 5 10 20

MAX = 4. “_” marks a slot that holds no live element.

How to write it in the exam

  • Both insertions first check full: (rear + 1) % MAX == front.
  • Insert at front moves front backward with wrap-around: front = (front − 1 + MAX) % MAX.
  • Insert at rear moves rear forward: rear = (rear + 1) % MAX.
  • Inserting into an empty deque sets front = rear = 0.
How this answer was verified
  • tests/linear-expression.test.ts
  • deque_array.c matches the engine on 200 random cases

Similar questions