Pregunta de entrevista de Palantir Technologies

Write a minPeak function for a stack (function that returns the minimum element in the stack).

Respuestas de entrevistas

Anónimo

26 de ene de 2015

Push on the previous lowest and update your own lowest. So insert a 4, now we have on the stack (4:nothing) and lowest is 4. Now insert a 7 - (4:Nothing), (7:4). Now insert a 3 -> (4:Nothing), (7:4), (3:4) and lowest is 3. Now insert a 9 -> (4:Nothing), (7:4), (3:4), (9:3). Now pop -> lowest is still 3 and stack is (4:Nothing), (7:4), (3:4) etc. When you push, you push on the previous lowest at the same spot, and when you pop, you update your lowest to the top of the stack's low.

1

Anónimo

19 de mar de 2013

Push one by one onto another stack while recording the min and then push it back onto the old stack.

1

Anónimo

6 de ene de 2015

If you push onto a queue instead of a stack, you will preserve the ordering of the elements within the stack.