Back to All Algorithms
Stack
Category: Data Structures | Time: O(1) | Space: O(n)
Visualization
Visual representation of the data structure
Enter commands to start stack visualization.
About Stack

A linear data structure that follows the Last-In, First-Out (LIFO) principle. Elements are added (pushed) and removed (popped) from the same end, called the 'top'.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Variable State
{
  "status": "Initializing stack"
}
Stack: How It Works
Last updated October 28, 2026

Overview

A stack is a Last-In, First-Out (LIFO) collection: the last item pushed is the first one popped, like a stack of plates. All operations happen at one end, called the top, which is why push, pop and peek are all O(1).

Stacks are everywhere once you start looking. Your program's call stack stores function frames this way; the undo button keeps a stack of edits; the back button keeps a stack of pages; and many algorithms, from depth-first search to bracket matching, use a stack to remember where to return to.

The default commands push 10, 20 and 30, pop (removing 30), push 40, then pop twice (removing 40 and then 20). Only 10 remains, at the bottom. Each pop always takes whatever was added most recently.

Step by step

  1. push(x): place x on top of the stack.
  2. pop(): remove and return the top element; signal an error (underflow) if the stack is empty.
  3. peek(): return the top element without removing it.
  4. isEmpty(): check whether the stack has no elements.

Complexity

CaseCostWhy
PushO(1)Amortised O(1) for a dynamic array; strict O(1) for a linked list.
Pop / peekO(1)Only the top is touched.
SearchO(n)A stack is not designed for searching.
SpaceO(n)One slot per element.

When to use it

  • Matching brackets, tags or parentheses (see the Valid Parentheses interview question).
  • Undo/redo, browser history, and backtracking algorithms.
  • Evaluating postfix expressions and converting infix to postfix.
  • Iterative depth-first search and replacing recursion when the call stack is too small.

Common mistakes

  • Popping from an empty stack — always check isEmpty() or handle undefined.
  • Using shift/unshift on a JavaScript array as the stack end; that is O(n). Use push/pop.
  • Unbounded recursion is a hidden stack overflow; deep inputs need an explicit stack.
Worked Example: Stack Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Stack: follow each row, predict the next one, and compare. The run below takes 16 steps in total. The Line column refers to the line numbers in the code listing above.

Input
push 10,push 20,push 30,pop,push 40,pop,pop
Result
status = Operations complete, final_stack = [10]
StepLineArrayWhat happens
12[]status = Initializing stack
24[]action = push(10)
35[10]status = Pushed 10, top = 10
44[10]action = push(20)
55[10, 20]status = Pushed 20, top = 20
64[10, 20]action = push(30)
75[10, 20, 30]status = Pushed 30, top = 30
87[10, 20, 30]action = pop()
911[10, 20]status = Popped 30, top = 20
104[10, 20]action = push(40)
115[10, 20, 40]status = Pushed 40, top = 40
127[10, 20, 40]action = pop()
1311[10, 20]status = Popped 40, top = 20
147[10, 20]action = pop()
1511[10]status = Popped 20, top = 10
1618[10]status = Operations complete, final_stack = [10]
Frequently Asked Questions
About Stack

What is a Stack?

A Stack is a linear data structure that follows a particular order in which the operations are performed. The order is Last-In, First-Out (LIFO). An excellent analogy is a stack of plates; you can only add a plate to the top and can only remove the top plate.

What are the primary operations of a Stack?

The primary operations are:
  • Push: Adds an element to the top of the stack.
  • Pop: Removes the top element from the stack.
  • Peek (or Top): Returns the top element without removing it.
  • isEmpty: Checks if the stack is empty.

What is the time complexity of stack operations?

For a standard implementation (e.g., using an array or a linked list), the time complexity for `push`, `pop`, and `peek` is O(1). These operations are very fast as they don't depend on the number of items in the stack.

Where are stacks used in computing?

Stacks are used in many areas:
  • Function Calls: The 'call stack' manages active function calls. When a function is called, it's pushed onto the stack, and when it returns, it's popped off.
  • Undo/Redo Features: Text editors and other software use stacks to implement undo functionality.
  • Expression Evaluation: Compilers use stacks to convert expressions from infix to postfix notation and evaluate them.
  • Backtracking Algorithms: Stacks are used to keep track of paths in algorithms like maze-solving or in graph traversals (DFS).

What is a stack overflow?

A stack overflow is an error that occurs when the call stack runs out of space. This usually happens with very deep or infinite recursion, where functions are pushed onto the stack faster than they are popped off.

What is a stack underflow?

A stack underflow occurs when you try to `pop` an element from an empty stack. A well-implemented stack should handle this case by throwing an error or returning a specific value like null or undefined.

How can a Stack be implemented?

A stack can be easily implemented using either an Array (or a dynamic array like a vector) or a Linked List. An array-based implementation is simple and cache-friendly, while a linked-list implementation offers more flexible memory management.

What's the difference between a Stack and a Queue?

The main difference is the order of operations. A Stack is **LIFO (Last-In, First-Out)**, while a Queue is **FIFO (First-In, First-Out)**. Think of a queue as a line of people; the first person to get in line is the first one to be served.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.
Related Interview Questions
Practice with real problems