Skip to main content

20. Min Stack

easyAsked at Salesforce

Design a stack supporting push, pop, top, and retrieving the minimum element in O(1). Salesforce uses this to test data-structure design with an auxiliary-stack twist.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Common Salesforce backend onsite design question.
  • LeetCode Discuss (2025-12)Salesforce tests this to gauge auxiliary-structure reasoning.

Problem

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class with push(int val), pop(), top(), and getMin().

Constraints

  • -2^31 <= val <= 2^31 - 1
  • Methods pop, top, and getMin operations will always be called on non-empty stacks.
  • At most 3 * 10^4 calls will be made.

Examples

Example 1

Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output
[null,null,null,null,-3,null,0,-2]

Approaches

1. Single stack + scan for min

Push/pop/top work on one stack. getMin scans the stack.

Time
O(n) for getMin, O(1) others
Space
O(n)
class MinStack {
  constructor() { this.stack = []; }
  push(v) { this.stack.push(v); }
  pop() { this.stack.pop(); }
  top() { return this.stack[this.stack.length - 1]; }
  getMin() { return Math.min(...this.stack); }
}

Tradeoff: Violates the O(1) getMin requirement. Salesforce will fail this.

2. Parallel min stack

Maintain a second stack of running minimums. On push, store min(val, current min). On pop, pop both.

Time
O(1) all operations
Space
O(n)
class MinStack {
  constructor() { this.stack = []; this.mins = []; }
  push(v) {
    this.stack.push(v);
    const m = this.mins.length ? Math.min(this.mins[this.mins.length - 1], v) : v;
    this.mins.push(m);
  }
  pop() { this.stack.pop(); this.mins.pop(); }
  top() { return this.stack[this.stack.length - 1]; }
  getMin() { return this.mins[this.mins.length - 1]; }
}

Tradeoff: O(1) all operations at the cost of O(n) auxiliary space. The mins stack always has the same length as the main stack; the top of mins is always the current overall min.

Salesforce-specific tips

Salesforce uses this to grade whether you reach for an auxiliary structure rather than try to be clever with a single array. Bonus signal: discuss the space-optimized variant (push only when val <= current min) which saves space on uniform inputs.

Common mistakes

  • Storing only the current overall min (not the per-state min) — when you pop, you lose the prior min.
  • Using Math.min(...arr) for getMin — that's O(n), not O(1).
  • Forgetting to pop both stacks together — corrupts the invariant.

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • Optimize space: push to mins stack only when val <= current min.
  • Max stack (LC 716) — symmetric problem.
  • Implement min queue using two min stacks.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why the parallel stack instead of a variable?

Because popping doesn't tell you the prior min — you'd need to scan. The parallel stack stores the min state at each level of the main stack.

Can I save space by only pushing to mins when val < currentMin?

Yes, but pop needs to check if the popped value equals the current min and only then pop mins. Slightly tricky — Salesforce sometimes asks this as a follow-up.

Practice these live with InterviewChamp.AI

Drill Min Stack and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →