← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Max Stack is a LeetCode problem (716) commonly asked in LinkedIn interviews, requiring design of a stack with extra max-finding capabilities using structures like doubly linked lists and heaps or TreeMaps.[1][3]
Design a MaxStack class supporting five operations:
void push(int x): Pushes element x onto the stack.int pop(): Removes and returns the top element (standard stack pop).int top(): Returns the top element without removing it.int peekMax(): Returns the current maximum element in the stack.int popMax(): Removes and returns the current maximum element (not necessarily at the top; if ties exist, remove the most recently added one).[3][5][1]popMax(), prioritize the most recent occurrence of the max value.[4][1]MaxStack stack = new MaxStack(); stack.push(5); // stack: [5], max: 5 stack.push(1); // stack: [5,1], max: 5 stack.push(5); // stack: [5,1,5], max: 5 stack.top(); // returns 5 (top) stack.popMax(); // removes second 5, stack: [5,1], returns 5 stack.top(); // returns 1 stack.peekMax(); // returns 5 stack.pop(); // removes 1, stack: [5], returns 1
This matches the sequence shown in multiple sources, where after pushes, popMax() targets the latest 5.[8][1][4]
-1000 <= x <= 1000