As reported: Salesforce LMTS backend loop (India, Sept 2025), round 1 (DSA, virtual): after a discussion of a challenging situation in a project, implement a max stack like LeetCode 155 — Min Stack, bring every operation down to
O(1), and write the full implementation with tests in Java; then the approach for Maximum Frequency Stack.
This is LeetCode 155 — Min Stack with the maximum instead of the minimum.
Implement MaxStack:
MaxStack()creates an empty stack.push(val)pushesval.pop()removes the top element.top()returns the top element.getMax()returns the largest element in the stack.
Every operation must run in O(1) time.
Example
["MaxStack","push","push","push","getMax","pop","top","getMax"]
[[],[-2],[0],[-3],[],[],[],[]]
Output: [null,null,null,null,0,null,0,0]
Constraints: -2^31 ≤ val ≤ 2^31 − 1; pop, top and getMax are only called on a non-empty stack; at most 3·10^4 calls.
Code runner format: two lines in LeetCode's design format: the method names, then their arguments. The harness prints every call's result as a JSON array (null for the constructor, push and pop).