Space Complexity
Code Analysis
Algorithm Efficiency
Computer Science
Programming

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.

Practice algorithms

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:

python
1def find_max(values):
2    best = values[0]
3    for value in values:
4        if value > best:
5            best = value
6    return best

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.

python
1def squares(values):
2    result = []
3    for value in values:
4        result.append(value * value)
5    return result

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.

python
1def factorial(n):
2    if n <= 1:
3        return 1
4    return n * factorial(n - 1)

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.

python
1def count_even(values):
2    count = 0
3    for value in values:
4        if value % 2 == 0:
5            count += 1
6    return count

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:

  1. are new arrays, lists, maps, or sets created,
  2. do they grow with input size,
  3. is recursion present,
  4. is the algorithm in place or does it copy data,
  5. 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.

python
1def reverse_in_place(values):
2    left, right = 0, len(values) - 1
3    while left < right:
4        values[left], values[right] = values[right], values[left]
5        left += 1
6        right -= 1

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.