Tutorial on space complexity of algorithms
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Space complexity describes how much memory an algorithm needs as the input grows. It is easy to focus only on runtime, but memory use often determines whether a solution can handle large inputs without crashing, swapping, or becoming impractical in production.
What Space Complexity Measures
When people say an algorithm uses O(n) space or O(1) space, they usually mean the amount of memory required relative to input size n. In many discussions, the most useful number is the extra memory beyond the input itself, which is often called auxiliary space.
That distinction matters:
- input space is the memory needed to hold the input data
- auxiliary space is the extra memory the algorithm allocates while it runs
If a function receives a list of one million numbers, that list already consumes memory before the algorithm starts. The algorithm's space complexity usually focuses on what it adds on top of that.
Constant Extra Space: O(1)
An algorithm has O(1) auxiliary space if it uses the same amount of additional memory no matter how large the input becomes.
This function uses a few variables, but the number of variables does not grow with the list size. The loop may take longer for larger inputs, yet the extra memory stays effectively constant.
Linear Extra Space: O(n)
An algorithm uses O(n) space when the extra memory grows in direct proportion to the input size. A common example is building a copy or transformed version of the input.
Here the result list grows with the input length, so the algorithm uses linear auxiliary space.
Hash tables and sets also often lead to O(n) space usage when they store information about every input element.
Recursive Algorithms Use Stack Space
Recursion can hide memory usage because the extra space is on the call stack rather than in a list or dictionary. Every recursive call adds a new stack frame, so the recursion depth affects space complexity.
This recursive factorial uses O(n) stack space because the call depth grows linearly with n.
Now compare that with iterative factorial:
The iterative version uses O(1) auxiliary space. Same output, different memory profile.
Some Algorithms Use O(log n) Space
Divide-and-conquer algorithms often use logarithmic stack space because the recursion depth grows with the height of the split tree rather than the full input size.
Binary search only recurses along one half at a time, so the maximum depth is O(log n).
In-Place Algorithms Reduce Space Usage
An in-place algorithm modifies the input structure instead of allocating another full-sized structure. That often reduces auxiliary space, though it can make the code less convenient or less safe when the original input must be preserved.
This uses O(1) extra space because it swaps inside the existing list rather than building a second one.
Why Space Complexity Matters in Practice
Memory limits appear in more places than people expect. Competitive programming platforms impose strict limits, mobile devices have tighter budgets than servers, and large in-memory data processing jobs can fail even when the runtime complexity looks acceptable.
Space complexity also affects performance indirectly. Extra allocations increase pressure on the garbage collector, reduce cache friendliness, and can force the system to spill data to disk. An O(n) space algorithm is not automatically bad, but it should be a deliberate choice.
Common Pitfalls
The most common misunderstanding is confusing total input size with auxiliary space and then labeling every algorithm O(n) just because the input itself exists. Another frequent mistake is forgetting the call stack when analyzing recursive code. Developers also call an algorithm in-place even when it quietly allocates helper structures proportional to the input. Finally, big-O space analysis can hide constant factors, which still matter when objects are large or memory is tight.
Summary
- Space complexity measures how memory usage grows as input size increases.
- Auxiliary space is usually the most useful number when comparing algorithms.
- Iterative solutions often use less memory than recursive ones because they avoid deep call stacks.
- In-place algorithms can reduce extra memory, but they modify the original data.
- Good algorithm analysis considers both time complexity and space complexity, not just one of them.
Related reading
- Two elements in array whose xor is maximum
- Two player grid traversal game
- Two salesmen - one always visits the nearest neighbour, the other the farthest
- UIImage - implementing an auto levels algorithm
- Two single-column indexes vs one two-column index in MySQL?
- Types in MySQL BigInt20 vs Int20
- Ukkonen's suffix tree algorithm in plain English
- Ukkonen's suffix tree algorithm in plain English

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.