Tuesday, February 28, 2012

Stack - push, pop, max operations in constant time

One good question I was asked in one of the interview in my career.
Implement Integer value Stack such a way that all the following operations are constant operation, i.e. O(1).
void push(int n) -> Pushes element n on the stack
int pop() -> Pops element from the top of the stack and return it.
int max() -> Returns maximum of all elements contained in the Stack at that moment.

Isn't it a good Question?