Amazon Interview Question

Design and Implement a data structure which supports push() pop() and min() in a constant time

Interview Answer

Anonymous

Aug 9, 2013

As there have been no limitations the stack could be implemented by... stack; while initially I thought to have just an extra cell for min, this was of course wrong. Another thought about heap was also wrong, as the minimum might be popped at some point, and the heap will become useless. The solution is to have... two stacks: one for the ... stack and one for the min. Te latter might have duplicates. push() will update both stacks: one with the pushed value and one for the minimum which is either the previous minimum (dup) or the new pushed one pop() will similarly pop from both stacks min() will just peek() the stack dedicated to minimum