Space Complexity
A measure of how much memory an algorithm requires as a function of its input size
Space complexity is a measure, expressed in Big O notation, of how much memory an algorithm requires to run as a function of the size of its input.
Definition
Space complexity is a measure, expressed in Big O notation, of how much memory an algorithm requires to run as a function of the size of its input.
Overview
Space complexity is the memory counterpart to time complexity: instead of counting how many operations an algorithm performs as input size n grows, it counts how much additional memory the algorithm needs. That memory includes both the input itself and any auxiliary storage the algorithm allocates while running — variables, data structures, and the call stack used by recursive functions. Because input storage is often unavoidable, analyses frequently separate out auxiliary space complexity, which counts only the extra memory beyond what's needed to hold the input, since that's usually the figure an engineer can actually influence through algorithm design. Recursive algorithms are a common place where space complexity surprises people: each recursive call adds a frame to the call stack, so an algorithm that looks like it uses no extra data structures can still have O(n) space complexity if it recurses n levels deep, as with an unoptimized recursive Fibonacci or a depth-first traversal of an unbalanced tree. This is why some recursive algorithms are deliberately rewritten iteratively with an explicit stack, or restructured to be tail-recursive in languages whose compilers optimize tail calls, trading code clarity for space efficiency. Space and time complexity often trade off against each other, and this trade-off is a recurring theme in algorithm design: memoization or caching reduces time complexity by storing previously computed results, but increases space complexity to hold that cache; in-place sorting algorithms like heapsort achieve O(1) auxiliary space by rearranging elements within the original array, while mergesort typically needs O(n) auxiliary space for its merge step but offers other advantages like stability. Understanding space complexity matters most acutely in memory-constrained environments — embedded systems, mobile devices, or processing datasets too large to fit entirely in RAM — where an algorithm with excellent time complexity can still be impractical if its space requirements exceed available memory.
Key Concepts
- Expressed in Big O notation as a function of input size n
- Distinguishes total space (including input) from auxiliary space
- Accounts for call stack usage in recursive algorithms
- Often trades off directly against time complexity
- In-place algorithms achieve O(1) auxiliary space by reusing input storage
- Memoization/caching trades increased space for reduced time complexity
- Critical constraint in memory-limited environments like embedded systems
- Commonly analyzed alongside time complexity in algorithm design and interviews