Data Structures

What is Stack?

A stack is a linear data structure that stores items in last-in, first-out (LIFO) order: the most recently added element is the first one removed. It supports three core operations — push (add to the top), pop (remove the top), and peek (read the top) — each running in constant time.

Time push, pop, peek, isEmpty, size: O(1) eachSpace O(n) for n stored elementsTopic Data Structures

The Core Idea: Last In, First Out

Picture a stack of plates. You add a plate to the top, and when you need one, you take it from the top. You cannot pull a plate from the middle without lifting everything above it. That is a stack: the last item you put in is the first item you get back — last in, first out (LIFO).

This single rule is what makes stacks useful. Whenever a problem has a 'the most recent thing must be handled first' shape — matching brackets, undoing edits, tracking which function called which — a stack captures that ordering for free. It is easiest to feel this when you watch items push on and pop off one at a time.

Push, Pop, and Peek

A stack exposes three operations. Push adds an element to the top. Pop removes and returns the top element. Peek (sometimes called top) reads the top without removing it. Most implementations also offer isEmpty and size. Because all the action happens at one end, each operation runs in O(1) — no shifting or searching required.

Under the hood, a stack is usually a thin wrapper over a dynamic array (a Python list or a JavaScript array) or a singly linked list. With an array you push and pop at the tail; with a linked list you add and remove at the head. The one case to guard is popping from an empty stack — 'stack underflow' — which should raise an error rather than fail silently.

What Stacks Are Good For

Four patterns cover most stack problems. Balanced-parentheses checking pushes each opening bracket and pops when it meets a closing one, verifying the pair matches. A monotonic stack keeps its elements in sorted order to answer 'next greater element' style questions in linear time. Depth-first search and recursion both lean on a stack — an explicit one or the language's call stack — to remember where to backtrack. And undo/redo in an editor is literally two stacks of past states.

pythonA stack backed by a Python list, with underflow guarded on pop and peek.
class Stack:
    def __init__(self):
        self._items = []

    def push(self, item):
        self._items.append(item)        # add to top, O(1)

    def pop(self):
        if not self._items:
            raise IndexError("pop from empty stack")
        return self._items.pop()        # remove top, O(1)

    def peek(self):
        if not self._items:
            raise IndexError("peek from empty stack")
        return self._items[-1]          # read top, no removal

    def is_empty(self):
        return not self._items

    def __len__(self):
        return len(self._items)
Time complexitypush, pop, peek, isEmpty, size: O(1) each
Space complexityO(n) for n stored elements

Array-backed push/pop are amortized O(1): an occasional resize costs O(n) but averages out over many operations. A linked-list backing gives true worst-case O(1) per operation at the cost of an extra pointer per node.

When to use it

  • Validating nested structure: parentheses, brackets, HTML/XML tags, or JSON.
  • Reversing a sequence, or processing items in the opposite order they arrived.
  • Implementing depth-first search, or converting a recursive algorithm into an iterative one.
  • Next-greater / next-smaller element and histogram problems solved with a monotonic stack.

Watch out for

  • Popping or peeking without first checking for an empty stack, which triggers an underflow error or an out-of-bounds read.
  • Reaching into the middle of the stack. If you find yourself indexing elements other than the top, a stack is probably the wrong tool — consider an array, deque, or heap.
  • Operating on the slow end. Pushing or popping at the front of a dynamic array is O(n); always work on the end that gives O(1).
In the interview

Stacks are interview bread and butter, especially at Indian placement drives and product-company loops. The classic warm-up is 'valid parentheses'; from there interviewers escalate to monotonic-stack problems (next greater element, daily temperatures, largest rectangle in a histogram), expression evaluation, and 'design a min-stack that returns the minimum in O(1)'. What they are really testing is whether you recognize the LIFO shape hidden inside a problem, whether you handle the empty-stack edge case cleanly, and whether you can state the O(1) time and O(n) space trade-offs. Being able to rewrite a recursive solution as an explicit stack is a strong signal that you understand how recursion works underneath.

Stack: frequently asked questions

Is a stack hard to learn?

No — the stack is one of the most approachable data structures. There is a single rule, LIFO, and three operations to remember: push, pop, and peek. The harder part is recognizing when a problem calls for a stack, which comes with practice on patterns like balanced brackets and monotonic stacks. Watching elements push and pop step by step makes it click fast.

What is the time complexity of stack operations?

Push, pop, peek, isEmpty, and size are all O(1). With an array-backed stack, push and pop are amortized O(1) because occasional resizing costs O(n) but averages out over many operations. Space is O(n) for n stored elements.

What is the difference between a stack and a queue?

A stack is LIFO — the last item in is the first out, and both add and remove happen at the same end. A queue is FIFO — the first item in is the first out, so you add at one end and remove from the other. Use a stack to reverse or backtrack, and a queue to process items in arrival order, like breadth-first search.

When should I use a stack instead of a plain array?

Reach for a stack when your access pattern is strictly last-in-first-out and you only ever touch the top. Naming it as a stack makes that intent obvious and prevents accidental middle access. If you genuinely need random access or need to work from both ends, use an array or a deque instead.

See Stack as an animated story

Reading the definition is one thing — watching stack run line by line, then explaining it to an AI interviewer, is how it actually sticks. That is what CodeStory does.