A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks…

2004

A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks grow from opposite ends of the array. Variables top1 and top2 (topl< 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” is

  1. A.

    (top1 = MAXSIZE/2) and (top2 = MAXSIZE/2+1)

  2. B.

    top1 + top2 = MAXSIZE

  3. C.

    (top1= MAXSIZE/2) or (top2 = MAXSIZE)

  4. D.

    top1= top2 -1

Attempted by 700 students.

Show answer & explanation

Correct answer: D

Correct condition: top1 = top2 - 1

Reasoning: Use a single array A[1..MAXSIZE] with two stacks growing from opposite ends. Let top1 point to the top element of the left stack and top2 point to the top element of the right stack.

  • Typical initialization (1-based indexing): top1 = 0 (left stack empty), top2 = MAXSIZE + 1 (right stack empty).

  • When an item is pushed onto the left stack, top1 is incremented; when pushed onto the right stack, top2 is decremented.

  • The array becomes full when there is no free index between the two stacks. That is when top1 + 1 = top2, which is equivalent to top1 = top2 - 1.

  • Other presented conditions are wrong because they either fix a midpoint (wasting space if one stack grows more) or state unrelated arithmetic on indices that do not guarantee adjacency.

Example: If top1 = 5 and top2 = 6 there is no free position between them, so the array is full.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir