Min Stack

easy · stack invariant

Implement MinStack(capacity); the nonnegative capacity argument is unused (the stack is unbounded). push(value) returns None. pop() returns the removed integer or None if empty. top() returns the most recent value or None if empty. get_min() returns the minimum current value or None if empty. All operations should take O(1) time, including repeated equal minimum values. Examples (JSON; null means Python None): Input: {"capacity": 0, "operations": [["push", [3]], ["push", [1]], ["get_min", []], ["pop", []], ["get_min", []]]} Output: [null, null, 1, 1, 3] Input: {"capacity": 0, "operations": [["pop", []], ["top", []], ["get_min", []]]} Output: [null, null, null]

Starter code

class MinStack:

    def __init__(self, capacity: int):
        """Implement this operation according to the statement."""
        pass

    def push(self, value: int) -> None:
        """Implement this operation according to the statement."""
        pass

    def pop(self) -> int | None:
        """Implement this operation according to the statement."""
        pass

    def top(self) -> int | None:
        """Implement this operation according to the statement."""
        pass

    def get_min(self) -> int | None:
        """Implement this operation according to the statement."""
        pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python interpreter

4 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. InterviewerCharacter joke · No posting date

    Walk me through your thinking. No, not out loud. In code. In silence. While I watch.

  2. Me at 2amCharacter joke · No posting date

    My code works. I have no idea why. I am afraid to touch it. I am going to bed.

  3. Rubber DuckCharacter joke · No posting date

    Quack. (Translation: have you tried printing it?)

  4. RecruiterCharacter joke · No posting date

    We loved your energy. We went with the candidate whose code compiled.