Space Complexity
Space complexity describes how the extra memory an algorithm uses grows with the input size. It is written in big-O, the same way as time, and it usually excludes the input itself.
Detailed Explanation
Time asks how many steps. Space asks how many extra bytes. The input array of n integers already occupies O(n) memory; that is not the algorithm's doing. Auxiliary space is the extra: the O(1) integers in a loop, the O(n) copy you sorted into, the O(h) stack frames of a recursive walk of height h.
In C the extra space has an address. Automatic arrays live on the call stack. malloc lives on the heap. Recursion lives on the stack, one frame per call. A function that declares int tmp[n]; as a VLA uses O(n) stack, which is a much smaller budget than O(n) heap. Quoting O(n) without saying which region hides a stack overflow.
In-place algorithms overwrite the input and aim for O(1) auxiliary space. Heapsort can be in-place. Mergesort of arrays needs an O(n) buffer for the merge, or it needs a linked-list formulation. Quicksort is in-place on the array and O(h) on the stack because of recursion; worst-case h is n.
Sharing matters. A graph algorithm that allocates visited[n], a queue of n integers, and an output array of n integers is O(n) still — constants drop — but the constants decide whether the program fits in RAM. Big-O tells you that doubling n doubles that working set. It does not tell you the program will fit.
Trade-offs are the point of measuring both. An O(n) table that turns an O(n^2) search into O(n) is a hash set of seen values. An O(n) prefix-sum array turns range-sum queries into O(1). You buy time with space, until the table no longer fits in cache and the constant factors reverse the win.
Report auxiliary space unless you say otherwise. Say "including the output" when the output is a new array of size n. Say "stack" when recursion is the consumer. Interviewers will ask.
Code Examples
#include <stdio.h>
/* O(1) auxiliary space: a few integers, no allocation, no recursion. */
static int sum(const int *a, size_t n)
{
int total = 0;
for (size_t i = 0; i < n; i++) {
total += a[i];
}
return total;
}
int main(void)
{
int a[] = { 1, 2, 3, 4 };
printf("%d\n", sum(a, 4)); /* prints: 10 */
return 0;
}#include <stdio.h>
#include <stdlib.h>
/* O(n) auxiliary heap: a copy. The input is not counted. */
static int *prefix(const int *a, size_t n)
{
int *p = malloc(n * sizeof *p);
if (p == NULL || n == 0) {
return p;
}
p[0] = a[0];
for (size_t i = 1; i < n; i++) {
p[i] = p[i - 1] + a[i];
}
return p;
}
int main(void)
{
int a[] = { 1, 2, 3 };
int *p = prefix(a, 3);
if (p == NULL) {
return 1;
}
printf("%d\n", p[2]); /* prints: 6 */
free(p);
return 0;
}Common Gotchas
Recursion's stack is space
an `O(n)`-depth DFS on a linked list is `O(n)` auxiliary stack, even with no `malloc`.
In-place is about auxiliary space, not about never writing the input
swapping elements still counts as `O(1)` extra.
A VLA of size `n` is `O(n)` on the stack
the same `O(n)` on the heap would be safer for large `n`.
Dropping constants can hide a cache miss
two `O(n)` buffers may evict the input from L1 and slow a "linear" pass by a large factor.