Medium · Stacks & Queues

Implement stack with min

Given a sequence of push(val), pop() and min() operations on an initially empty stack, return in order the value returned by every pop() and min() call, using 0 when the stack is empty.

Examples

Example 1

{
  "ops": [
    {"kind": "push", "val": 5},
    {"kind": "push", "val": 3},
    {"kind": "push", "val": 7},
    {"kind": "push", "val": 2},
    {"kind": "min"},
    {"kind": "pop"},
    {"kind": "min"}
  ]
}

Output: [2, 2, 3]

Example 2

{
  "ops": [
    {"kind": "push", "val": 4},
    {"kind": "push", "val": 1},
    {"kind": "push", "val": 1},
    {"kind": "min"},
    {"kind": "pop"},
    {"kind": "pop"},
    {"kind": "min"}
  ]
}

Output: [1, 1, 1, 4]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Stacks & Queues problems