Complexity Analysis

Space Complexity

Space complexity measures how much extra memory an algorithm needs as input grows. It is often traded against time — faster algorithms frequently require more RAM.

What is space complexity?

Space complexity measures how much extra memory an algorithm needs as the input grows. Swapping two variables uses O(1) space, copying an array uses O(n), and a recursive function uses O(depth) space for its call stack. Often you can trade space for time, for example by storing results in a hash map to avoid recomputing them.

const reverseInPlace = (a) => {        // O(1) extra space
  for (let i = 0, j = a.length - 1; i < j; i++, j--) [a[i], a[j]] = [a[j], a[i]];
  return a;
};
const reversedCopy = (a) => [...a].reverse();   // O(n) extra space

Interactive growth visualiser

Use the slider to see how different space complexities grow with n. O(1) algorithms are ideal — they use the same memory regardless of input size.

n =

16
O(1)
1
O(log n)
4
O(n)
16
O(n log n)
64
O(n²)
256

Auxiliary vs total space

Total space = input size + auxiliary space. Auxiliary space = extra memory beyond the input itself. Interviews usually ask about auxiliary space — we already know the input exists.

arr[0]0x00
arr[1]0x04
arr[2]0x08
left=00x0C
right=20x10

In-place reverse: only 2 extra variables (O(1) auxiliary space). The input array is not counted.

Common space complexities

In-place algorithms — constant extra varsO(1)

Constant time

Binary search recursion — log n stack framesO(log n)

Logarithmic time

Copy of input, hash map, recursion depth nO(n)

Linear time

Merge sort auxiliary array + recursionO(n log n)

Linearithmic time

2D matrix, adjacency matrix for n nodesO(n²)

Quadratic time

In-place vs copying

space-tradeoff.pseudo
1// In-place reverse — O(1) auxiliary space
2function reverse(arr):
3 left = 0, right = len(arr) - 1
4 while left < right:
5 swap(arr[left], arr[right])
6 left++, right--
7 
8// O(n) space version — creates a copy
9function reverseCopy(arr):
10 result = new Array(len(arr))
11 for i from 0 to len(arr) - 1:
12 result[i] = arr[len(arr) - 1 - i]
13 return result

Recursion call stack

recursion-space.pseudo
1// Recursion depth = call stack frames
2function factorial(n):
3 if n <= 1: return 1
4 return n * factorial(n - 1) // O(n) stack frames
5 
6// Tail-recursive — compiler can optimise to O(1) space
7function factTail(n, acc = 1):
8 if n <= 1: return acc
9 return factTail(n - 1, n * acc)
🔍Time-space trade-off
Memoization converts O(2ⁿ) time to O(n) time — at the cost of O(n) space. Compression converts O(n) space to O(1) — at the cost of O(n) decompression time. Neither is universally better; the right trade depends on your bottleneck.

🎯 Quick Check

What is the auxiliary space complexity of merge sort on an array of n elements?

Space Complexity: summary

Key points

  • Usually only auxiliary (extra) space is counted, not the input itself.
  • Recursion depth counts: a recursive sum of n numbers uses O(n) stack space.
  • Memoisation trades O(n) memory for a big drop in running time.

Frequently asked questions

›Does recursion use more memory than a loop?

Yes. Each recursive call adds a stack frame, so a recursion n levels deep uses O(n) space, while an equivalent loop uses O(1).