FastPrepMinimum Stack

Minimum Stack

Snowflake logoSnowflake● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement a stack that supports pushing, popping, reading its top value, and reading its minimum value.

Process operations in order, starting with an empty stack. The parallel array values supplies the value for each push; ignore its entry for every other operation.

  • push: put values[i] on top.
  • pop: remove the top value.
  • top: report the top value without changing the stack.
  • getMin: report the smallest value currently in the stack without changing it.

Return one decimal string for each top or getMin query, in query order. Push and pop produce no output. Duplicate values are separate stack entries. Every pop or query is guaranteed to occur when the stack is nonempty. An empty operation list, or a list with no queries, returns an empty array.

Aim for constant-time operations by maintaining the current minimum alongside each stack position.

Function

minStack(operations: String[], values: int[]) → String[]

Examples

Example 1

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

The minimum is -3 before popping. Removing -3 restores the previous minimum -2 while the top is 0.

Example 2

operations = ["push","push","getMin","pop","getMin"]values = [5,5,0,0,0]return = ["5","5"]

The two 5 values are distinct entries. Popping one does not remove the other minimum.

Constraints

  • 0 <= operations.length = values.length <= 10000.
  • Every operation is exactly push, pop, top, or getMin.
  • Each value is a signed 32-bit integer, from -2147483648 through 2147483647.
  • Pop, top, and minimum queries only occur on nonempty stacks.

More Snowflake problems

See Snowflake hiring insights
Saved
public String[] minStack(String[] operations, int[] values) {
    // Write your solution here.
}
operations["push","push","push","getMin","pop","top","getMin"]
values[-2,0,-3,0,0,0,0]
expected["-3", "0", "-2"]
Checking account…