Skip to content
Official PYQ · 2004

GATE CS 2004 Q3 — Arrays, Stacks and Queues

Explain with AI

A single array A[1…MAXSIZE]A[1 \ldots \text{MAXSIZE}] is used to implement two stacks. The two stacks grow from opposite ends of the array. Variables top1top1 and top2top2 (top1<top2)(top1 < top 2) point to the location of the topmost element in each of the stacks. If the space is to be used efficiently, the condition for “stack full”\text{“}\textsf{stack full}\text{”} is

Choose one option
View worked solution

Answer: D

To utilize the array space efficiently, we should allow both stacks to grow dynamically into the shared space. This means one stack can occupy more than half of the array if the other stack is relatively small.

Let the array be A[1…MAXSIZE]A[1 \ldots \text{MAXSIZE}].

  • Stack 1 starts from the left end (11) and grows towards the right (1,2,3,…1, 2, 3, \ldots). Its topmost element is pointed to by top1top1.
  • Stack 2 starts from the right end (MAXSIZE\text{MAXSIZE}) and grows towards the left (MAXSIZE,MAXSIZE−1,…\text{MAXSIZE}, \text{MAXSIZE}-1, \ldots). Its topmost element is pointed to by top2top2.

Since the stacks grow towards each other, the array becomes completely full when there are no empty slots left between top1top1 and top2top2. Since top1<top2top1 < top2, this boundary condition occurs when the two pointers are adjacent:

top1+1=top2  ⟹  top1=top2−1top1 + 1 = top2 \implies top1 = top2 - 1

Analysis of Options:

  • Option A & C: These force a static division of space (e.g., limiting each stack to half the array size), which defeats the purpose of efficient space utilization.
  • Option B: top1+top2=MAXSIZEtop1 + top2 = \text{MAXSIZE} is incorrect. For example, if MAXSIZE=10\text{MAXSIZE} = 10, top1=3top1 = 3, and top2=7top2 = 7, then top1+top2=10top1 + top2 = 10, but slots 4,5,4, 5, and 66 are still empty.
  • Option D: top1=top2−1top1 = top2 - 1 correctly identifies that the two stacks have met and no free space remains.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions