Min Stack
Python data engineering interview problem. Difficulty: intermediate. Pattern: Stacks. About 14 minutes. Part of the Pro drill bank.
Stack that also retrieves the current minimum in O(1). Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A monitoring stack needs the newest value and also the smallest value currently stored, without scanning every item each time. Implement MinStack with push, pop, top and getMin. Every operation should return or update the right value, and getMin should be constant time. Write class MinStack. After pushing -2, 0 and -3, the minimum is -3; after popping -3, the top and minimum are both 0 or -2 according to the remaining stack. The starter file already declares the class shape and harness prints. Keep the class name, method names and print lines, and fill in the methods.
Requirements
- Keep normal stack last-in, first-out behavior.
- Return the current minimum in O(1).
- Update minimum state when the minimum value is pushed or popped.
Constraints
- Values can be negative and duplicates may occur.
- The harness calls methods in the supplied order, so keep the class API unchanged.
Examples
Input: push -2,0,-3; getMin; pop; top; getMin Output: -3, 0, -2 Min updates after pop.
Topics: lakebench, python, min, stack.
More Python interview questions · All interview problems · Learn data engineering
Min Stack
Interview-style drill: Stack that also retrieves the current minimum in O(1).
Implement class `MinStack` with `push(x)`, `pop()`, `top()`, and `getMin()`. Keep the harness prints.