I came across this interesting question while reading 'Cracking the coding Interview' and came up with a simple implementation. Any suggestions are welcomed.

Imagine a (literal) stack of plates. If the stack gets too high, it might topple.
Therefore, in real life, we would likely start a new stack when the previous stack
exceeds some threshold. Implement a data structure SetOf Stacks that mimics
this. SetOf Stacks should be composed of several stacks and should create a
new stack once the previous one exceeds capacity. SetOf Stacks. push() and
SetOf Stacks. pop() should behave identically to a single stack (that is, pop()
should return the same values as it would if there were just a single stack).

My take was to use a LinkedListy Stack which stores reference to Stack that is currently active, and once it exceeds it's capacity (i.e. stack full) it'll create another stack and change the pointer (like linkedlist)

1 Answer
1

Inconsistent API

This is an inconsistent API for a stack:

interface Stack{
public void push(int ... data);
public Node pop();
}

That is, pushing int values but popping Node values is strange.

Perhaps the problem originates from your trying to use the same interface for both stacks: the internal stack of stacks, and the user-facing stack of integers.
One way to do that would be using a generic interface:

interface Stack<E> {
void push(E data);
E pop();
}

Encapsulate / hide implementation details

Node is an implementation detail of your stack.
As far as this problem is concerned,
the stack should appear as a regular stack.
The fact that internally it's using multiple stacks, linked lists, nodes,
should be hidden,
as they are not the concern of the users.

Naming conventions in Java

In Java, the convention is that classes should be named with PascalCase,
so BasicStack instead of Stack.

Linked list of linked lists?

Just for the record,
the exercise is probably motivated by a problem concerning stack implementations backed by a single array.
Growing or shrinking arrays are expensive operations (allocate new array, copy from old array),
which can be greatly mitigated by using a stack of arrays,
as described in the exercises.

Keep in mind that stack implementations backed by a linked list don't have the same issue, and growing and shrinking the storage are cheap operations. As such, stacking linked lists is quite pointless.