AlgoViz

O(n) linear space

easy 14 min

O(n) space means the extra memory an algorithm needs grows in proportion to its input: double n and the memory doubles.

Where it shows up

The same pattern appears across everyday software.

  • Caches in front of databases

    Web services keep a hash map (Redis, Memcached, or an in-process dictionary) of recent query results. The cache grows with the number of distinct keys it holds, which is linear space spent to turn slow database calls into instant lookups.

  • Deduplicating events

    Payment and messaging pipelines keep a set of event IDs they have already processed so a retried message is not handled twice. The set grows by one ID per event, and is usually bounded by a time window to cap the memory.

  • Undo history in editors

    Text and image editors store one record per action so you can undo. The history is a stack that grows linearly with the number of edits, which is why many editors cap it at a fixed number of steps.

  • Web crawlers and graph search

    A crawler keeps a visited set of URLs and a queue of pages to fetch. Both can hold a large share of all pages seen, so memory grows with the size of the crawl.

  • Immutable data and copies

    Frameworks such as React encourage creating a new array or object instead of changing the old one. Each update can cost O(n) space for the copy, traded for simpler change detection and safe sharing.