What is the space complexity of this code?
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 is about how memory usage grows with input size, not about how many variable names you can count in a function. The tricky part is separating fixed overhead from memory that scales with n. When people ask “what is the space complexity of this code,” the correct method is to inspect what additional storage the algorithm creates and how recursion, data structures, and outputs grow relative to the input.
Start With The Right Question
A useful first question is not “how many variables are there?” but rather:
- what memory already belongs to the input,
- what extra memory does the algorithm allocate,
- does recursion add stack frames,
- does the returned result itself grow with input size.
That is how you avoid superficial analysis.
Example 1: Constant Auxiliary Space
Consider this function:
The input list may be large, but the algorithm creates only a small fixed number of extra variables. That means its auxiliary space complexity is O(1).
The important distinction is that the input itself is not usually counted as extra space because it already exists.
Example 2: Linear Extra Space
Now compare that with code that builds a new list.
Here result grows with the number of input elements. If the input has n values, the function allocates storage proportional to n, so the auxiliary space is O(n).
This is the classic pattern people should look for first: does the algorithm create a growing container?
Recursion Changes The Answer
Recursion can add hidden memory usage through the call stack.
Although no list or map is allocated, the recursion depth is n, so the stack space is O(n).
That is why a short-looking function can still have linear space complexity.
Output Space Versus Auxiliary Space
Sometimes people count the output structure; sometimes they ask only for auxiliary space. Those answers differ.
For the squares example:
- auxiliary space:
O(n), because of the result list, - total space including output: also
O(n).
But in some problems, the output must exist no matter what algorithm you choose. In those cases, interviewers often care more about auxiliary space than total memory footprint.
So if the question is ambiguous, say which convention you are using.
Loops Do Not Automatically Mean More Space
A loop can run n times while still using constant extra memory.
This is O(1) auxiliary space even though it takes O(n) time. Time complexity and space complexity are related but separate measurements.
A Practical Analysis Checklist
When analyzing code, inspect these in order:
- are new arrays, lists, maps, or sets created,
- do they grow with input size,
- is recursion present,
- is the algorithm in place or does it copy data,
- are you counting output space or only auxiliary space.
This checklist usually gets you to the right answer faster than line-by-line variable counting.
In-Place Algorithms Often Improve Space
For example, an in-place array reversal typically uses only a few temporary variables.
This is O(1) auxiliary space because it modifies the input rather than allocating another full array.
Common Pitfalls
- Counting the input as extra memory instead of focusing on additional allocated space.
- Ignoring recursion stack usage.
- Assuming every loop implies
O(n)space. - Forgetting to clarify whether output storage counts toward the answer.
- Counting variable names instead of analyzing whether memory usage scales with input size.
Summary
- Space complexity measures how memory usage grows with input size.
- Focus on extra allocated memory, recursion depth, and growing containers.
- A loop alone does not imply non-constant space.
- Recursion can create linear stack space even without explicit containers.
- Always clarify whether you mean total space or auxiliary space when the question is ambiguous.
Related reading
- What is the state of the art in computer chess tree searching?
- What is the time and space complexity of a breadth first and depth first tree traversal?
- What is the time complexity of heapq.nlargest?
- What is the time complexity of java.util.HashMap class' keySet method?
- What is the time complexity big O of sortedArrayUsingComparator? iOS/OSX
- What is the time complexity of k-means?
- What is the time complexity of my function?
- What is the time complexity of Ruby's built in permutation and repeated_permutation methods?

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.