Home / DSA / Lesson 4

Stacks and queues

Beginner 8 min read Lesson 4 of 9

Stack — last in, first out. Push and pop at one end. Use cases: undo, DFS, matching brackets, the language call stack (each function call pushes a frame).

Queue — first in, first out. Use cases: print queues, BFS level-order.

In JS, an array can be a stack (push/pop). A queue with shift is O(n); for heavy BFS you would use a deque, but service-company problems are usually small.

Judge: Valid parentheses. Pattern: Stack patterns, BFS.