A single array is used to implement two stacks. The two stacks grow from opposite ends of the array. Variables and point to the location of the topmost element in each of the stacks. If the space is to be used efficiently, the condition for is
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 .
- Stack 1 starts from the left end () and grows towards the right (). Its topmost element is pointed to by .
- Stack 2 starts from the right end () and grows towards the left (). Its topmost element is pointed to by .
Since the stacks grow towards each other, the array becomes completely full when there are no empty slots left between and . Since , this boundary condition occurs when the two pointers are adjacent:
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: is incorrect. For example, if , , and , then , but slots and are still empty.
- Option D: correctly identifies that the two stacks have met and no free space remains.
Free practice · Your answers on this page are not saved.
