As reported: Cohesity MTS-2 coding round (2025): Min Stack. A brute-force version first, then the expected complexity, full code with edge cases checked before running, then a follow-up. The feedback stressed working out the space complexity quickly and covering all edge cases.
This is LeetCode 155 — Min Stack. Our write-up: /min-stack.
Design a stack that supports push, pop, top, and retrieving the minimum element, each in O(1) time.
MinStack()initialises the stack.void push(int val)pushesval.void pop()removes the top element.int top()returns the top element.int getMin()returns the minimum element in the stack.
Example
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output: [null,null,null,null,-3,null,0,-2]
Constraints: -2^31 <= val <= 2^31 - 1; pop, top and getMin are only called on a non-empty stack; at most 3 * 10^4 calls in total.
Code runner format: two lines, LeetCode's design format: the method names, then the argument lists. The output lists each call's result (null for the constructor, push and pop).