Problem Submissions Solution

Implement Stack Using Array

Difficulty: Basic

Acceptance: %

Points: 10.00

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:

  1. Type 1 x: Pushes the integer x onto the stack.
  2. 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:
Visa Goldman Sachs Qualcomm
Topics:
Stack
Locked Content
Access Restricted: Please Login to access the code editor and test cases.