DSA-VerseLearn by watching code run
DashboardData StructuresAlgorithmsVisualizerCORECoding PlaygroundInterview QuestionsComplexity AnalyzerLearning NotesBookmarksLearning ProgressSettings
All data structures

Linear Data Structures

Stacks

A Last-In-First-Out (LIFO) collection: push and pop happen only at the top.

Interactive1 visualizer(s)

1Introduction

A stack restricts access to a single end. That restriction is exactly what makes it useful: it naturally remembers 'the most recent unfinished thing', which is how function calls, undo and nested brackets work.

2What is it?

A stack is a linear collection with two main operations: push (add to the top) and pop (remove from the top). peek returns the top without removing it. The last element pushed is the first one popped — LIFO.

3Why do we use it?

  • O(1) push, pop and peek.
  • Models nested structure: function calls, parentheses, HTML tags.
  • Turns recursion into iteration (explicit stack).
  • Supports undo/redo and backtracking.

4Real-world examples

  • A stack of plates.
  • Browser back button.
  • Undo in a text editor.
  • The program call stack.

5Internal working

Array-based stacks keep an index `top`; push writes at top + 1, pop reads at top and decrements it.

Linked-list stacks push and pop at the head node.

With a fixed capacity, push on a full stack is an overflow; pop on an empty stack is an underflow.

6Memory representation

Array-backed stack with capacity 4 after push 10, push 20, push 30:

index   value
  3   |       |
  2   |  30   |  <- top (index 2)
  1   |  20   |
  0   |  10   |
      +-------+

7Operations

OperationHow it works
push(x)Place x on top.
pop()Remove and return the top.
peek()Read the top.
isEmpty()Check size = 0.
search(x)Not a stack operation — needs O(n) scan.

8Visual explanation

Stack Push and Popinteractive Open full visualizer
push 10push 20push 30push 40pop()pop()pop()pop()
push
10
0
1
2
top = −1 (empty)

Size 0 / 3 · LIFO — the last value pushed is the first one popped

Top elementJust pushedPopped / rejected

A stack is Last-In-First-Out (LIFO): push adds to the top, pop removes from the top. A fixed capacity shows overflow (push when full) and underflow (pop when empty).

1 / 24

9Code implementation

Complete, runnable programs (verified in all four languages). JavaScript and Python can be executed in the Coding Playground.

class Stack {
  constructor(capacity) {
    this.items = [];
    this.capacity = capacity;
  }

  push(value) {
    if (this.items.length === this.capacity) {
      console.log("Stack Overflow: cannot push " + value);
      return;
    }
    this.items.push(value);
  }

  pop() {
    if (this.items.length === 0) {
      console.log("Stack Underflow: nothing to pop");
      return null;
    }
    return this.items.pop();
  }
}

const stack = new Stack(3);
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
console.log("Popped:", stack.pop());
console.log("Popped:", stack.pop());
console.log("Popped:", stack.pop());
console.log("Popped:", stack.pop());
console.log("Final stack (bottom -> top):", stack.items);

10Time complexity

OperationTime
push(x)O(1)*
pop()O(1)
peek()O(1)
isEmpty()O(1)
search(x)O(n)

11Space complexity

A stack holding n elements uses O(n) space. *Dynamic-array stacks occasionally resize (O(n) for that push) but are O(1) amortized.

12Advantages

  • Constant-time operations.
  • Simple to implement with an array or a list.
  • Matches recursive/nested problems perfectly.

13Disadvantages

  • Only the top is accessible.
  • Fixed-capacity stacks can overflow.
  • Deep recursion can overflow the call stack.

14Common mistakes

  • Popping or peeking without checking for empty (underflow).
  • Using a queue where LIFO order is needed (or vice versa).
  • In JavaScript, using shift/unshift (O(n)) instead of push/pop.
  • Forgetting that recursion depth is limited (stack overflow).

15Interview questions

Valid ParenthesesEasyImplement Queue using StacksEasy

16Practice exercises

  1. Implement a min-stack with O(1) getMin.
  2. Evaluate a postfix (RPN) expression.
  3. Next greater element for every array item.
  4. Reverse a string using a stack.
  5. Convert infix to postfix (shunting-yard).

Try them in the Coding Playground.

On this page

  1. 01Introduction
  2. 02What is it?
  3. 03Why do we use it?
  4. 04Real-world examples
  5. 05Internal working
  6. 06Memory representation
  7. 07Operations
  8. 08Visual explanation
  9. 09Code implementation
  10. 10Time complexity
  11. 11Space complexity
  12. 12Advantages
  13. 13Disadvantages
  14. 14Common mistakes
  15. 15Interview questions
  16. 16Practice exercises