AlgoViz

Space complexity compared

easy 16 min

Space complexity measures how much extra memory an algorithm needs as the input grows.

Where it shows up

The same pattern appears across everyday software.

  • Mobile apps under a memory cap

    Phones kill apps that use too much memory. Image galleries decode thumbnails instead of full photos and recycle list rows, keeping memory near constant however long the list.

  • Social graphs as adjacency lists

    A network with a billion users and a few hundred friends each could never be an n by n matrix (10^18 cells). Adjacency lists store only the edges that exist: O(V + E).

  • Streaming analytics in constant space

    Counting page views or averaging sensor readings over an endless stream keeps a running total and count, O(1) memory. Sketches such as HyperLogLog estimate unique visitors in a few kilobytes.

  • Diff tools and DNA alignment

    Comparing two long files or genomes is an LCS-style DP. A full table for two million-line inputs is impossible, so tools keep a band or two rows and use divide-and-conquer to recover the alignment in linear space.

  • Stack overflows in production

    Deeply recursive code (a JSON parser on deeply nested input, a recursive tree walk on a degenerate tree) can crash with a stack overflow even though the heap has plenty of room. Iterative versions with an explicit stack avoid it.