FastPrepAdding Stack with Top Values

Adding Stack with Top Values

IMC Trading logoIMC Trading● MediumNEW GRADOA
Learn

Problem statement

Implement a stack that starts empty and processes the commands in operations:

  • push v: add integer v to the top.
  • pop: remove the top.
  • inc i v: add v to each of the bottom i elements.

Return one string per command: the current top as a decimal integer, or EMPTY if the stack is empty. Each command should take constant amortized time. Use signed64-bit arithmetic for stack values.

Function

superStack(operations: String[]) → String[]

Examples

Example 1

operations = ["push 4","push 5","inc 2 1","pop","pop"]return = ["4","5","6","5","EMPTY"]

Report the current top after each operation, including EMPTY when the stack becomes empty.

Example 2

operations = ["push 2","push 8","inc 1 3","pop","push -1","pop","pop"]return = ["2","8","8","5","-1","5","EMPTY"]

Report the current top after each operation, including EMPTY when the stack becomes empty.

Constraints

  • 1 <= operations.length <= 2 * 10^5
  • -10^9 <= v <= 10^9
  • For inc i v, 1 <= i <= current stack size.
  • pop is never called on an empty stack.

More IMC Trading problems

See IMC Trading hiring insights
public String[] superStack(String[] operations) {
    // Write your solution here.
}
operations["push 4","push 5","inc 2 1","pop","pop"]
expected["4", "5", "6", "5", "EMPTY"]
Checking account…