FastPrepMin Stack

Min Stack

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Process operations on a stack that can return its current minimum in constant time.

  • push x pushes integer x and produces no output.
  • pop removes and returns the top value.
  • top returns the top value without removing it.
  • getMin returns the smallest value currently in the stack.

Return the outputs of every operation except push, in encounter order.

Function

processMinStack(operations: String[]) → int[]

Examples

Example 1

operations = ["push -2","push 0","push -3","getMin","pop","top","getMin"]return = [-3,-3,0,-2]

The minimum becomes -3. Popping returns -3, exposing top 0 and minimum -2.

Example 2

operations = ["push 2","push 2","getMin","pop","getMin"]return = [2,2,2]

Duplicate minima are tracked independently, so one pop leaves the same minimum.

Constraints

  • 1 <= operations.length <= 10^5.
  • Pushed values are 32-bit signed integers.
  • Every pop, top, and getMin operation is issued on a nonempty stack.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] processMinStack(String[] operations) {
  // Write your code here.
}
operations["push -2","push 0","push -3","getMin","pop","top","getMin"]
expected[-3,-3,0,-2]
Checking account…