FunctionRacerProblemsRace

Stack After Commands

BeginnerStacks and queuesPython, JavaScript, TypeScript, Java, C++

commands is a list of strings. Each one is either "push X", where X is an integer (it may be negative), or "pop".

Start with an empty stack and apply the commands in order: push X puts X on top of the stack and pop removes the top item. A pop on an empty stack does nothing.

Return the final stack as a list from bottom to top, so the oldest item still on the stack comes first. Return an empty list if the stack ends up empty.

Example 1

commands ["push 3","push 5","pop","push 7"]
returns [3,7]

After push 3, push 5, pop the stack holds 3; then push 7 puts 7 on top.

Example 2

commands ["pop","push 1","push 2"]
returns [1,2]

The first pop finds an empty stack and does nothing; then 1 and 2 are pushed.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems