O(1) Space Complexity Meaning
Fast recall
Quick revision
Core answer
Whether you understand constant space means fixed regardless of n, and can give a real example
Calling something O(1) space while allocating a new array/list that scales with n
Contrast in-place (O(1)) with copy-based (O(n)) and note recursion breaks O(1) via stack depth
Interview-ready guide
Interview answer
O of one space, or constant space, means the extra memory my algorithm uses doesn't grow with the input size.
I'm using just a fixed handful of variables, like a couple of counters, a temp, or two pointers, and that amount is the same whether the input has ten elements or ten million. A real example is summing an array.
I keep a single accumulator variable and add each element into it, so no matter how big the array is, I only ever use that one variable, which is O of one space. Reversing an array in place is another good one: I use two pointers, one at each end, and swap, again just a couple of variables. Kadane's algorithm for maximum subarray sum is O of one space too.
The contrast is anything that allocates a structure that scales with n, like copying the array into a new array, building a HashMap of all the elements, or returning a result list sized to the input. Those are O of n space. Two subtleties I'd flag.
First, by space I mean auxiliary space, so the input array itself doesn't count, only what I add on top. Second, recursion has a hidden cost: even if a recursive function doesn't allocate any data structures, each pending call sits on the stack, so a recursion that's n levels deep is actually O of n space. That means an in-place algorithm written recursively isn't truly constant space.
When memory is the constraint, I deliberately favor iterative, in-place approaches to stay at O of one.
O(1) Space Complexity Meaning
Direct Answer
O(1) space (constant space) means the extra memory an algorithm uses does not grow with the input - just a fixed set of variables. Reversing an array in place with two pointers, or summing with a single accumulator, is O(1) space. It's the opposite of allocating an O(n) helper array or HashMap. Note: the input itself isn't counted, only the auxiliary memory.
Code Example
// O(1) space: one accumulator, in-place pointers
int sum(int[] a) {
int total = 0; // single variable
for (int x : a) total += x;
return total;
}
void reverseInPlace(int[] a) {
int i = 0, j = a.length - 1; // two pointers - constant space
while (i < j) { int t = a[i]; a[i++] = a[j]; a[j--] = t; }
}
// O(n) space: allocates a structure that grows with n
int[] reverseCopy(int[] a) {
int[] out = new int[a.length]; // grows with n -> O(n)
for (int i = 0; i < a.length; i++) out[i] = a[a.length - 1 - i];
return out;
}
Common Mistakes & Pitfalls
- Allocating then claiming O(1): a
new int[n]or HashMap makes it O(n) space. - Forgetting recursion stack: 'in-place but recursive' is O(depth), not O(1).
- Counting the input: O(1) space refers to auxiliary memory, not the input array.
- Output not counted vs counted: if you must return an O(n) structure, that's usually O(n) space.
Check your understanding
Answer these without looking back at the article.
- What does in-place algorithm mean?
- Can a sorting algorithm be O(n log n) time and O(1) space?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.