Help with two stacks in one array exercise solution

Hi,

I am taking Part 1 of the Data Structures course and just wanted to ask about the exercise to implement two stacks in one array.

I am quite puzzled about the solution provided in the TwoStacks.java file.

I undertand that the solution is memory efficient, because it shares the same array positions for both stacks, but can’t imagine a practical use for it.

In which case would I want to use two stacks that share the same array positions in that way?

It is somewhat contrived, but basically it would be some heavily memory constrained application. Or perhaps some situation where the stacks had a logical relationship.

Maybe this is a undo / redo stack setup. As you undo stuff it goes to the redo stack and as you redo stuff it goes to the undo stack. New actions go into the undo stack (and clears the redo stack). That’s just one contrived example I could think of.