Pergunta de entrevista da empresa Nagarro

Create a stack that does push, pop and getMax operations in O(1) time. getMax should return max of all elements in the stack.

Resposta da entrevista

Sigiloso

19 de dez. de 2019

I created 2 stacks. One was for elements and another was keeping max. Whenever an element is pushed to stack then max(maxStack.peek(), element) is pushed to the maxStack. We can do push and pop operations in O(1) time and also getMax operation in O(1).