Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-001

Stack Push and Pop Simulation

Problem

Simulate an array-based stack. Given a list of operations where each operation is either "push" or "pop", and a parallel list values, execute them in order.

For a push, append values[i] to the stack and record None. For a pop, remove the top element and record it, or record None if the stack is empty.

Return the list of recorded results.

Hint

A Python list already behaves like a stack: append for push, pop for pop.

Test cases

case 1
in: [["push","push","pop"],[1,2,0]]
expected: [null,null,2]
case 2
in: [["pop"],[0]]
expected: [null]
case 3
in: [["push","pop","pop"],[5,0,0]]
expected: [null,5,null]
case 4
in: [["push","push","push","pop","pop"],[1,2,3,0,0]]
expected: [null,null,null,3,2]
Starter codePython
def stack_ops(operations, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor