Sub topic 3
Writing space usage in Big O notation
This chapter assumes you know basic Python, lists and dictionaries, and introductory Big O for time. Here you will learn to turn "how much memory does this use?" into a Big O statement.
From bytes to growth rates
Space complexity counts memory as a function of input size n. Exact byte counts change with the interpreter and the platform. Big O keeps only the growth rate, so it ignores those details.
The same rules you use for time apply to space:
- Drop constant factors. Storing 2n items is O(n).
- Drop lower-order terms. Storing n + 10 items is O(n).
- Keep the dominant term. Storing n² + n items is O(n²).
- Add memory that is live at the same time. Take the maximum of memory that is used one stage after another and freed in between.
Common space classes
| Class | Meaning | Typical example |
|---|---|---|
| O(1) | Fixed number of variables | Running total in a loop |
| O(log n) | Memory grows with the logarithm of n | Recursive binary search stack |
| O(n) | One item per input item | A new list the size of the input |
| O(n²) | One item per pair of inputs | An n by n table |
Reading Big O from code
Compare these two functions. Both loop over n numbers, but they hold different amounts of memory.
def total(nums):
s = 0
for x in nums:
s += x
return s
def doubled(nums):
result = []
for x in nums:
result.append(2 * x)
return resulttotal keeps one integer, s, and one loop variable, x. The count of variables does not change with n, so the space is O(1). doubled builds result, which ends with n items. Its space is O(n).
Both functions run in O(n) time. Space and time are separate measurements, and one does not determine the other.
Recursion counts too
Memory is not only the variables you name. Each active function call occupies a stack frame, and the frame stays until the call returns.
def sum_recursive(nums, i=0):
if i == len(nums):
return 0
return nums[i] + sum_recursive(nums, i + 1)This code creates no list. Yet the calls nest to depth n before any of them returns, so the auxiliary space is O(n).
Common mistakes
- Writing O(2n) or O(n + 1). Write O(n) and O(n).
- Counting the input itself when the question asks about extra memory. The next chapter separates the two.
- Forgetting the stack in recursive code.
- Confusing time with space. A loop that runs n times may still use O(1) space.
- Reporting the final memory instead of the peak. Big O space describes the most memory held at one moment.
Exercise
A function takes a list of n numbers and builds a dictionary that maps each number to its square. Another function takes the same list and returns the largest number using one variable. Give the auxiliary space of each in Big O, and say what one thing each answer depends on.
Show answer
The dictionary function uses O(n) space, because it stores one entry per input number. This assumes the numbers are distinct, and duplicates would only reduce the count, which still leaves O(n) as the worst case. The largest-number function uses O(1) space, because it keeps one variable no matter how long the list is. Each answer depends on what grows with n.