Write a program to implement a Stack using an array. Complete the push() and pop() methods as described below. The push(x) method takes an integer x as input and adds it to the stack. The pop() method removes and returns the top element of the stack. If the stack is empty, the pop() method should return -1.
Input is provided as a series of queries:
Type 1 x: Pushes the integer x onto the stack.
Type 2: Pops the top element from the stack and prints it. If the stack is empty, it prints -1.
Topics
Companies
Articles
Examples:
Expected Time Complexity: O(1)
Expected Space Complexity: O(1)
Constraints:
1 <= numbers of calls made to push, pop <= 100
1 <= x <= 100
Companies:
VisaGoldman SachsQualcomm
Topics:
Stack
Please log in to access the Submissions tab, where you can review your progress and explore code submissions from other participants.
Please log in to access the Solution tab and view detailed answers and explanations.
Please log in to access the Discussion tab and join conversations with other participants.
Access Restricted: Please Login to access the code editor and test cases.