Stack of Numeric Edits That Reports Its Minimum Instantly

Last updated: November 29, 2026

Quick Overview

Implement a stack with push, pop, top and get_min, where every operation runs in O(1) time. It backs an editor that tracks the lowest bid entered so far across nested undo steps.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Implement a stack with push, pop, top and get_min, where every operation runs in O(1) time. It backs an editor that tracks the lowest bid entered so far across nested undo steps.

A classic short design question that Meta uses early in a screen to see whether you can maintain an invariant alongside a structure. The interviewer looks for the per-entry minimum trick and clean handling of empty-stack calls.

What the Interviewer Expects
  • Explain why a single min variable fails after pops
  • Store the running minimum alongside each element
  • Keep every operation O(1)
  • Handle empty-stack calls explicitly
  • Mention the space-saving variant with a separate min stack
Key Topics to Cover
Stack
Invariant maintenance
Auxiliary state
Data structure design
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Can you reduce extra space when many pushes do not change the minimum?
  • How would you also support get_max?
  • Can you do it with O(1) extra space by storing encoded differences?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • push(v), pop(), top(), get_min() all O(1).
  • Values are integers and may repeat.
  • Calls on an empty stack should raise rather than return garbage....
High-Level Design

A single min field cannot be restored after the minimum is popped, because we would not know the previous minimum. The fix is to store, with each element, the minimum of the stack up to and includin...


Submit Your Answer
Markdown supported