Min Stack

Algorithms
Medium
Meta
126.5K views

Design a stack that supports push, pop, top, and retrieving the minimum element in $O(1)$ time.

Why Interviewers Ask This

Meta evaluates this question to assess a candidate's ability to optimize space-time trade-offs in constrained environments. They specifically look for understanding of how auxiliary data structures can maintain state without degrading performance, testing your grasp of amortized analysis and stack manipulation logic rather than just brute-force iteration.

How to Answer This Question

1. Clarify requirements: Confirm if the stack handles duplicates and what happens on empty pop operations. 2. Propose a brute-force solution first to establish a baseline O(n) complexity for min retrieval, acknowledging its inefficiency. 3. Introduce the optimal two-stack approach: one for standard elements and one auxiliary stack tracking minimums at every step. 4. Detail the push logic: Push new values onto the main stack; push onto the min stack only if the new value is less than or equal to the current min. 5. Explain pop logic: Pop from both stacks simultaneously if the popped value matches the current min, ensuring the auxiliary stack always reflects the true minimum. 6. Verify time complexity: Confirm that push, pop, top, and getMin all operate in constant O(1) time with O(n) space usage, aligning with Meta's focus on efficient system design.

Key Points to Cover

  • Demonstrating awareness of the O(1) constraint requirement
  • Proposing a specific two-stack auxiliary data structure solution
  • Correctly handling duplicate minimum values during push operations
  • Ensuring synchronized popping logic to maintain stack integrity
  • Articulating the space-time trade-off clearly

Sample Answer

To solve the Min Stack problem efficiently, I would implement a dual-stack architecture. First, I'll initialize a primary stack to store all incoming integers and a secondary stack to track the running minimums. When pushing a value, I add it to the primary stack immediately. Crucially, for the secondary stack, I compare the new value against the current top of the minimum stack. If the new value is smaller than or equal to the current minimum, I push it onto the secondary stack as well. This ensures the top of the secondary stack always holds the global minimum. For the pop operation, I remove the element from the primary stack. If this removed element equals the current minimum (the top of the secondary stack), I also pop from the secondary stack to reveal the next smallest value. The top and getMin operations simply return the tops of their respective stacks, guaranteeing O(1) access. This approach uses O(n) extra space but strictly maintains O(1) time complexity for all four required operations. This method demonstrates my ability to balance memory usage against computational speed, a key consideration in Meta's high-scale infrastructure where latency matters.

Common Mistakes to Avoid

  • Attempting to iterate through the entire stack to find the minimum, resulting in O(n) time complexity
  • Failing to handle duplicate minimum values correctly, causing the min stack to become desynchronized
  • Not clarifying edge cases like popping when the stack contains only one element before starting
  • Overlooking the need for an auxiliary stack and trying to modify the original stack directly

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 71 Meta questions