Skip to content
BENIBIX Learn

Space Complexity

Development🧮 Algorithms & Data StructuresAlgorithm Basics

Also known as Memory Complexity

How much extra memory a task needs. It grows with the input.

Think of it like

Think of it like a recipe. A bigger cake needs more bowls.

Example

Jack's first version copied a 2 GB file into memory to count its lines. Reading it one line at a time cut the memory to a few kilobytes.

Why it matters

Memory runs out before time does on phones, small servers and huge data sets.

Definition

Space complexity is how much extra memory an algorithm needs as its input grows. Like time complexity, it is written in Big O. Sorting a list in place may need O(1) extra memory. Making a full copy of the list needs O(n). Often you can trade one for the other: use more memory to save time, or the reverse.

Common mistake

Counting only time. A fast algorithm that needs ten times the memory can crash on real data.

Key takeaways

  1. 1
    Extra memory. Beyond the input itself.
  2. 2
    Written in Big O. O(1), O(n) and so on.
  3. 3
    Trade with time. More memory, fewer steps.

Compare