GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 3

Two stacks sharing an array

Data Structures · Stacks & Queues · Full GATE CSE 2004 paper

GATE 2004, Q3 1 mark · MCQ
Report issue

Stacks & Queues

Question 3, GATE 2004: Two stacks share A[1..MAXSIZE], growing from opposite ends. top1<top2 identify their topmost occupied cells. What full condition uses the available space efficiently?
Options:
(A) top1=MAXSIZE/2 AND top2=MAXSIZE/2+1
(B) top1+top2=MAXSIZE
(C) top1=MAXSIZE/2 OR top2=MAXSIZE
(D) top1=top2−1
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. The unused positions lie strictly between top1 and top2. There is no free position exactly when the tops are adjacent, top1=top2−1, D.

2. This lets either stack use all available space instead of reserving half the array.

3. Teaching check: The number of unused cells is top2−top1−1. Setting this to zero immediately gives the full condition. A fixed half-array boundary can falsely declare overflow while the other stack leaves unused cells, defeating the purpose of sharing capacity.

Original paper, page 1