AlgoViz

O(n²) quadratic space

easy 14 min

O(n²) space means memory grows with n × n: a 2D table or matrix with one cell for every pair.

Where it shows up

The same pattern appears across everyday software.

  • DNA and protein alignment

    Needleman-Wunsch and Smith-Waterman fill a score table over every pair of positions in two sequences. For long genomes the full table is far too big, so tools keep only a band of it or use Hirschberg's linear-space method to recover the alignment.

  • Spell checking and fuzzy search

    Edit distance between what you typed and dictionary words ranks the suggestions. Each comparison is small, so a table of a few hundred cells per word is cheap, and the cost only needs two rows.

  • Text diff tools

    Comparing two files is a longest-common-subsequence problem over lines. A naive O(n·m) table is too big for large files, which is why tools like git use Myers' algorithm and linear-space refinements instead.

  • Network routing and travel times

    Small networks and logistics tools precompute a distance matrix between every pair of locations so a lookup is O(1). It is O(V²) memory, fine for a few thousand nodes and impossible for a continent's road graph.

  • Images and game boards

    A photo is a 2D array of pixels and a game map is a 2D grid of tiles. A 4,000 × 4,000 image is 16 million pixels, so doubling the resolution quadruples the memory.