Pergunta de entrevista da empresa Amazon

Implement a data structure like a stack but with a way to find a max at O(1) time.

Respostas da entrevista

Sigiloso

8 de mar. de 2013

Later found out this was in Cracking the Coding Interviews.

Sigiloso

12 de mar. de 2013

There are several ways to do this. You push two numbers onto the stack whenever you add, first pushing the new number and then the max of the new number and whatever was below it. If you are allowed to store the max value in a field in the stack implementation you can write a normal stack implementation, except when a new value is equal to or greater than the old max, first push the old max onto the stack. then when you call get max you get pop until the value = the max you stored, and the next value you pop is the new maximum.