NeetCode
PracticeCoursesRoadmap
Sign InPro
PracticeCoursesRoadmap
Sign InPro
CoursesAlgorithms & Data Structures for Beginners

Progress

5/35

About

  • 0IntroductionFREE1m

Arrays

  • 1RAMFREE6m
  • 2Static Arrays15m
  • 3Dynamic Arrays16m
  • 4Stacks4m

Linked Lists

  • 5Singly Linked ListsFREE12m
  • 6Doubly Linked Lists10m
  • 7Queues4m

Recursion

  • 8Factorial11m
  • 9Fibonacci Sequence13m

Sorting

  • 10Insertion Sort19m
  • 11Merge Sort22m
  • 12Quick Sort17m
  • 13Bucket Sort14m

Binary Search

  • 14Search Array16m
  • 15Search Range8m

Trees

  • 16Binary Tree11m
  • 17Binary Search Tree15m
  • 18BST Insert and Remove22m
  • 19Depth-First Search15m
  • 20Breadth-First Search11m
  • 21BST Sets and Maps6m

Backtracking

  • 22Tree Maze14m

Heap / Priority Queue

  • 23Heap Properties14m
  • 24Push and Pop18m
  • 25Heapify15m

Hashing

  • 26Hash Usage10m
  • 27Hash Implementation29m

Graphs

  • 28Intro to Graphs22m
  • 29Matrix DFS22m
  • 30Matrix BFS14m
  • 31Adjacency List20m

Dynamic Programming

  • 321-Dimension DP20m
  • 332-Dimension DP22m

Bit Manipulation

  • 34Bit Operations17m

Arrays · Chapter 4 · 4 min

Stacks

A stack is a last-in, first-out (LIFO) collection. Think of a pile of plates: you can only add a plate to the top, and you can only take a plate from the top.

Operations

Stacks expose just a handful of operations:

  • push(x) — add x to the top
  • pop() — remove and return the top element
  • peek() — look at the top element without removing it
  • isEmpty() — check if the stack has any elements

Every operation is O(1).

Implementation

The easiest way to implement a stack is with a dynamic array. Append to the end for push, pop the end for pop. That's it.

stack = []
stack.append(1)   # push
stack.append(2)
stack.append(3)
print(stack[-1])  # peek → 3
print(stack.pop()) # pop → 3

Why Stacks Matter

Stacks show up everywhere in programming:

  • Function calls — the call stack is literally a stack
  • Undo/redo — each action is pushed, undo pops
  • Parsing — matching brackets and parentheses
  • Depth-first search — we'll see this in the trees section

Note: Anything that naturally "nests" is usually a stack problem.

Previous

Dynamic Arrays

Next

Singly Linked Lists

On this page