144. Implement Stack using Arrays

Implement a Last-In-First-Out (LIFO) stack using an array. The implemented stack should support the following operations: push, pop, peek, and isEmpty.

You will be provided two arrays operations which contains what operation need to perform and nums which contains the values corresponding to the operations.

Implement the ArrayStack class:

void push(int x): Pushes element x onto the stack.

int pop(): Removes and returns the top element of the stack.

int top(): Returns the top element of the stack without removing it.

boolean isEmpty(): Returns true if the stack is empty, false otherwise.

Please note that this section might seem a bit difficult without prior knowledge on what stacks is, we will soon try to add basics concepts for your ease! If you know the concepts already please go ahead to give a shot to the problem. Cheers!

Example 1:

Input: operations = ["ArrayStack", "push", "push", "top", "pop", "isEmpty"]

nums = [[], [5], [10], [], [], []]

Output: [null, null, null, 10, 10, false]

Explanation:

ArrayStack stack = new ArrayStack();

stack.push(5);

stack.push(10);

stack.top(); // returns 10

stack.pop(); // returns 10

stack.isEmpty(); // returns false

Example 2:

Input: operations = ["ArrayStack","isEmpty", "push", "pop", "isEmpty"]

nums = [[], [], [1], [], []]

Output: [null, true, null, 1, true]

Explanation: 

ArrayStack stack = new ArrayStack();

stack.push(1);

stack.pop(); // returns 1

stack.isEmpty(); // returns true

Now Your Turn!

Pick the correct output for the given input

Input: operations = ["ArrayStack", "isEmpty"]

nums = [[], []]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= numbers of calls made <= 100
  • 1 <= x <= 100

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class ArrayStack {
public:
ArrayStack() {
}
void push(int x) {
}
int pop() {
}
int top() {
}
bool isEmpty() {
}
};
Test Case

Input:

Nums
Operations