Space complexity analysis in Data Structures Theory - Time & Space Complexity
We want to understand how much extra memory a program uses as the input size grows.
The question is: how does the space needed change when the input gets bigger?
Analyze the time complexity of the following code snippet.
function createArray(n) {
let arr = [];
for (let i = 0; i < n; i++) {
arr.push(i);
}
return arr;
}
This code creates an array and fills it with numbers from 0 up to n-1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding one item to the array inside a loop.
- How many times: Exactly n times, once for each number from 0 to n-1.
As n grows, the array grows by one item each time the loop runs.
| Input Size (n) | Approx. Memory Used (items) |
|---|---|
| 10 | 10 items |
| 100 | 100 items |
| 1000 | 1000 items |
Pattern observation: Memory use grows directly with input size; doubling n doubles memory needed.
Time Complexity: O(n)
This means the memory needed grows in a straight line as the input size grows.
[X] Wrong: "The memory used stays the same no matter how big n is."
[OK] Correct: Since the array stores n items, more input means more memory is needed.
Understanding how memory grows with input helps you write programs that use resources wisely and avoid crashes.
What if the code created a two-dimensional array with n rows and n columns? How would the memory use change?
Practice
Solution
Step 1: Understand the definition of space complexity
Space complexity refers to how much memory or storage an algorithm needs while running, especially as the input size increases.Step 2: Differentiate from time complexity
Time complexity measures how long an algorithm takes, not memory usage. Hence, space complexity is about memory.Final Answer:
The amount of memory the algorithm uses as input size grows -> Option DQuick Check:
Space complexity = memory usage [OK]
- Confusing space complexity with time complexity
- Thinking it measures steps or errors
- Ignoring memory usage in the definition
Solution
Step 1: Recall Big O notation for constant space
Constant space means the algorithm uses the same amount of memory regardless of input size.Step 2: Identify the notation for constant space
O(1) means constant space, O(n) means linear, O(n^2) quadratic, and O(log n) logarithmic space.Final Answer:
O(1) -> Option BQuick Check:
Constant space = O(1) [OK]
- Choosing O(n) for constant space
- Confusing time and space complexity notations
- Misunderstanding logarithmic space
function example(arr):
temp = []
for i in arr:
temp.append(i)
return tempWhat is the space complexity of this function?
Solution
Step 1: Analyze memory usage for the temporary list
The function creates a new list 'temp' and appends each element from input 'arr', so 'temp' grows linearly with input size n.Step 2: Determine space complexity based on 'temp'
Since 'temp' stores all elements, space used is proportional to n, so space complexity is O(n).Final Answer:
O(n) -> Option CQuick Check:
New list size = input size n -> O(n) space [OK]
- Assuming constant space because no nested loops
- Confusing time complexity with space complexity
- Ignoring the temporary list's size
def process(n):
result = 0
for i in range(n):
temp = [0] * n
result += sum(temp)
return resultWhat is the space complexity and what is the mistake?
Solution
Step 1: Analyze space used by 'temp' inside the loop
Each iteration creates a list 'temp' of size n, but it is discarded after the iteration ends, so peak memory is O(n).Step 2: Identify the mistake in memory usage
Recreating 'temp' inside the loop wastes memory repeatedly; better to create once outside the loop.Final Answer:
O(n), mistake: temp list is recreated inside loop causing high memory use -> Option AQuick Check:
Repeated list creation inside loop = O(n) space [OK]
- Thinking space is O(1) because temp is local
- Assuming temp grows each iteration (it doesn't)
- Confusing time complexity with space complexity
Solution
Step 1: Understand the input structure
The input is an n x n matrix (list of n lists, each of size n), so total elements are n^2.Step 2: Analyze the output list size
The output list stores one sum per inner list, so it has n elements, which means space complexity is O(n).Final Answer:
O(n) -> Option AQuick Check:
One sum per inner list -> O(n) space [OK]
- Assuming output space is O(n^2) like input
- Confusing input size with output size
- Ignoring that sums reduce data size
Ready to prove it?
Pass the test and this pattern moves unknown
