Theory
A stack follows the Last In, First Out principle. The last element inserted is the first element removed. Common stack operations are push and pop. Stacks are used in function calls, undo operations, expression evaluation and browser history.
Example
const stack = [];
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.pop());
console.log(stack);
Expected Output
30 is removed first because the stack follows LIFO.
Practice
Implement a stack using an array with push and pop operations.
Interview Questions
- What is a stack?
- What does LIFO mean?
- What are push and pop?
- Where are stacks used?