Skip to content
Data Structures & Algorithmsbeginner · 35 min

Stacks & Queues

Prerequisites: Arrays & Strings

LIFO (stack) and FIFO (queue) access disciplines. They power expression parsing, backtracking, BFS, and the call stack itself. Know when each ordering is the right tool.

Stack (LIFO)

Push and pop happen at the same end, so the last item in is the first out. Stacks model nested structure: matching brackets, undo history, depth-first traversal, and the function call stack. All operations are O(1).

Queue (FIFO) & deque

Items leave in arrival order. Queues drive breadth-first search and task scheduling. A naive array `shift()` is O(n); use a linked list or two-stack/index-based ring buffer for O(1) dequeue. A deque allows O(1) at both ends and underlies sliding-window-maximum solutions.

Resources

Practice & test yourself

Take the quiz →