Skip to interview prep
Apexed

87. Stack With Constant-Time Minimum

MediumStacks

Simulate a stack that supports push, pop, top and getMin, each in O(1) time. You receive two arrays of equal length: ops (operation names) and values (values[i] is the number to push when ops[i] is "push", otherwise null).

Return an array with one entry per operation: null for "push" and "pop", the top value for "top", and the smallest value currently on the stack for "getMin".

"pop", "top" and "getMin" are never called on an empty stack.

Example 1

Input: ops = ["push","push","push","getMin","pop","top","getMin"], values = [-2, 0, -3, null, null, null, null]
Output: [null, null, null, -3, null, 0, -2]

Constraints

  • 1 ≤ ops.length ≤ 10⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Stack With Constant-Time Minimum — coding interview practice — Apexed