Stack Push and Pop
Stack Push and Pop
EasyStacks9% accepted
Simulate a stack with PUSH and POP operations. A stack is LIFO — Last In, First Out. Given Q operations, each either PUSH X (add X to top) or POP (remove the top element and print it), process all operations. If POP is called on an empty stack, print -1.
Approach
Given Q operations (PUSH X or POP), simulate a stack and print the result of each POP.
Input
4
PUSH 1
PUSH 2
POP
POP
Output
2 1
More Stacks problems
Browse all coding problems →solution.py
Loading editor...
Test 1
Test 2
Test 3
Press Run to test your custom input, or Submit to judge against all test cases.
Sign in to continue
Create a free account to run code, submit solutions and track progress.
- Free forever — no credit card
- 10+ languages with live execution
- Points, streaks & leaderboards