Connecteam interview question

Add a max() method to the Stack data structure with O(1) time complexity

Interview Answer

Anonymous

23 Aug 2022

when inserting with .push(), if the number is larger than the current max, push to a max numbers stack, when calling .max() return the .top() of the max stack. and when doing .pop() in the main stack, if the pop-ed number in the current max, pop it from both stacks, this ensures that the .top() of the max stack is always the max of the regular stack.