Home › Company-wise questions › PayPal › Min Stack ← PayPal bank
PayPal · Question 42 · Stacks
Min Stack Stacks Java code ⏱ O(1) 💾 O(n)
Product-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Approach Use two stacks: one to store all elements, another to keep track of minimums.
Time O(1)Space O(n)
How it works 1 Mirror new minima on a parallel stack. Sample input / output Input:
ops = [push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()]
Output:
[-3, 0, -2] Constraints Methods on non-empty stack; 32-bit values.
Solution — read it, then walk it Flowchart Pseudocode
Time O(1)Space O(n)Branches 2source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
START import java.util.* DEFINE class Main DEFINE class MinStack DEFINE Stack<Integer> stack DEFINE Stack<Integer> minStack DEFINE MinStack() stack = new Stack<>() minStack = new Stack<>() DEFINE void push(int val) stack.push(val) if (minStack.isEmpty() || val <= minStack.peek()… DEFINE void pop() if (stack.peek().equals(minStack.peek())) minStack.pop() stack.pop() DEFINE int top() { return stack.peek(); } DEFINE int getMin() { return minStack.peek(); } DEFINE static void main(String[] args) MinStack ms = new MinStack() ms.push(-2) ms.push(0) ms.push(-3) DEFINE a = ms.getMin() ms.pop() DEFINE b = ms.top() DEFINE c = ms.getMin() System.out.println("[" + a + ", " + b + ", " + c + "]") END start / end statement condition loop returns definition← Q41 · Valid Parentheses Q43 · Implement Queue using Stacks → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →