Space complexity
Пространственная сложность
A measure of how an algorithm’s memory needs change as its input gets larger. Keeping a copy of a list takes more space as the list grows; keeping a few counters does not.
Also known as: сложность по памяти
What space complexity measures
Space complexity describes how an algorithm’s peak memory requirement scales with input size. If n is the number of items in a list, a second list with one entry per item needs O(n) space. A fixed number of array indices needs O(1) space in the usual model where each index fits in one machine word. Big O gives a growth bound, not a byte count: two algorithms with the same space complexity can still have very different memory footprints.
Decide what to count
In coding interviews, people often mean auxiliary space: the working memory an algorithm needs aside from the supplied input. Count temporary collections, buffers, local variables, and the call stack. State whether you are counting the output. For example, an algorithm that fills a new n-element output array may use O(1) working space apart from that array, while still needing O(n) space for the result. A total-space estimate also accounts for the input.
Count what must be kept at the same time, not every allocation over the course of a run. Reusing a fixed-size buffer across n iterations takes constant working space; keeping one buffer from each iteration takes linear space. The same principle applies to recursion: if each call uses constant space, the stack cost depends on how many calls are active at once, not how many calls are made in total.
Example: the call stack can change the answer
def contains_sorted(items, target):
lo, hi = 0, len(items) - 1
while lo <= hi:
mid = (lo + hi) // 2
if items[mid] == target:
return True
if items[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return False
This binary search assumes items is a sorted array or list with constant-time indexing. It keeps a few indices and never copies the input, so it uses O(1) auxiliary space. A recursive version that passes the same list and two bounds can keep O(log n) stack frames active in the worst case if frames are not reused. Both versions take O(log n) time in the worst case under the usual assumption that each comparison takes constant time.
Why it matters in practice
A streaming approach can keep memory use low when processing a large file. Counting records needs only the current record and a counter, assuming bounded record size and a fixed-width counter. Counting distinct values with a set is different: even if you read one record at a time, the set can grow to hold every distinct value. Streaming controls how much input you keep, but it does not automatically bound the memory used by your algorithm’s state.
How to explain it in an interview
Say what n represents, distinguish working space from output space, and name what takes up the most memory at the peak. Check whether a library call makes a copy or whether a slice creates a new collection rather than a view. If you claim O(1) space, explain why no retained collection or stack grows with the input.