Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Stack

Стек

A collection that gives you back the most recently added item first. Like a stack of plates, you add an item at the top and take the next one from that same end.

What makes a collection a stack?

A stack is defined by the order in which items come out, not by how they are stored. That order is last in, first out (LIFO). If you push A, B, and C, the next item you can remove is C. You have to remove it before you can reach B. A queue follows the opposite rule for that same input: A comes out first because it arrived first.

Operations and implementation

push adds an item to the top. pop removes and returns the top item, while peek (sometimes called top) reads it without changing the stack. What happens when you pop an empty stack is part of the API contract: an implementation might raise an error or return a special value. If that value is also valid data, use an emptiness check or an API that reports success separately.

A resizable array makes a good stack when the top is at the array's end. Reading the last item takes O(1), and pushing takes amortized O(1): most pushes are cheap, even though a push that grows the backing array can take O(n). With a singly linked list, put the top at the head rather than the tail. Then a push or pop changes only a constant number of links, so both take O(1).

Example: matching brackets

When checking nested brackets, the next closing bracket has to match the most recent unmatched opener. A stack keeps that opener at the top. This Python function expects only bracket characters:

def brackets_match(text):
    expected = {'(': ')', '[': ']', '{': '}'}
    pending = []
    for char in text:
        if char in expected:
            pending.append(expected[char])
        elif not pending or pending.pop() != char:
            return False
    return not pending

The stack stores the closing brackets we expect to see. After reading ([, the top is ], so a ) at that point is a mismatch. A closing bracket with nothing pending is also invalid, as is any input that ends while a closing bracket is still pending. The check takes O(n) time and uses O(n) extra space in the worst case.

Where stacks show up

Undo starts with the latest edit, so a text editor can keep edits on a stack. An iterative depth-first search can use a stack to track nodes it still needs to visit. A call stack uses the same ordering for function calls: the most recent active call finishes before execution returns to its caller. But a stack you create in an algorithm is not necessarily stored in call-stack memory. LIFO is a rule about access, not a rule about memory allocation.

What interviewers look for

Beyond implementing push and pop, you may be asked to build a stack that reports its minimum in O(1). The key is to explain what stays true after every operation. For example, each entry can store the minimum from the bottom up to that entry. Popping restores the previous minimum automatically, even when several entries share the same smallest value.