Space complexity
Space complexity is how extra memory an algorithm uses as the input size n grows. It counts auxiliary storage, not the input itself, unless we say "total space". Recursion uses stack space; vector copies use heap space.
Detailed Explanation
Time says how long. Space says how much RAM. An algorithm that is O(n) time and O(1) extra space (in-place reverse of a vector) is a different product from one that is O(n) time and O(n) space (copy into a new vector). On an embedded target, space is the limit. On a server, an extra O(n) array of ints is usually fine; an extra O(n²) table is not.
We usually quote auxiliary space: what the algorithm allocates beyond the input. Sorting a vector with std::sort is O(1) or O(log n) extra depending on the implementation (introsort uses O(log n) stack). std::stable_sort may allocate O(n). std::merge into a new container is O(n) extra by definition.
The call stack counts. A recursive DFS on a chain of n nodes uses O(n) stack frames. C++ does not guarantee tail-call elimination, so a "O(1) extra" recursive function is often O(n) stack in practice. An explicit std::stack on the heap is also O(n) but will not blow the thread's fixed stack (often 1–8 MB).
Containers you create are space. A set of n ints is O(n) nodes, each larger than an int. A vector<bool> is about n/8 bytes. A 2D vector<vector<int>> of n×n is O(n²) plus n heap headers.
In-place means O(1) extra (or O(log n) for the sort stack) and overwrite of the input. Interviewers ask for it when they want you to notice you do not need a second array. Production code should prefer clear O(n) extra over a clever in-place algorithm that is unreadable, unless the allocation actually matters.
Trade-offs are the point. Memoised DP uses O(n) or O(n²) tables to turn exponential time into polynomial time. A hash index uses O(n) space to turn O(n) lookups into O(1). Prefix sums use O(n) space (or overwrite the array) to answer range queries in O(1).
When you document an API, mention extra allocations if they can surprise: string::substr allocates; string_view::substr does not. vector::insert may reallocate the whole buffer.
Code Examples
#include <algorithm>
#include <iostream>
#include <vector>
void reverseInPlace(std::vector<int>& v) { // O(1) extra
std::reverse(v.begin(), v.end());
}
std::vector<int> reversedCopy(std::vector<int> v) { // O(n) extra (the copy)
std::reverse(v.begin(), v.end());
return v;
}
int main() {
std::vector<int> a{1, 2, 3};
reverseInPlace(a);
std::cout << a[0] << '\n'; // prints: 3
auto b = reversedCopy({1, 2, 3});
std::cout << b[0] << '\n'; // prints: 3
}int treeHeight(int n) { // O(n) stack if the tree is a chain
if (n <= 0) return 0;
return 1 + treeHeight(n - 1);
}
int main() {
return treeHeight(3) == 3 ? 0 : 1;
}Common Gotchas
Recursion uses the thread stack, not the heap
`treeHeight(10'000'000)` will overflow. Use a loop or an explicit heap stack.
Auxiliary versus total
saying `sort` is O(n) space because the vector already exists confuses people. Quote extra space.
Node-based containers multiply constants
O(n) `map` nodes can be 4–8× the bytes of O(n) `vector` of pairs.
Hidden copies in pass-by-value
`void f(std::vector<int> v)` is O(n) extra before the body runs. Take `const&` if you only read.