time complexity
space complexity
algorithm analysis
computational efficiency
computer science basics

Difference between time complexity and space complexity

Codemia Knowledge Hub ·

Time complexity describes how much work an algorithm performs as the input size grows. Space complexity describes how much additional memory it needs beyond the input itself. Both are measures of cost, but they track different resources and often trade off against each other.

Time complexity: growth in work

Time complexity asks how running time scales as n grows. It usually does not mean wall-clock seconds on one machine; it counts the important operations relative to input size.

A linear scan over a list is the classic O(n) example:

python
1def contains_value(items, target):
2    for item in items:
3        if item == target:
4            return True
5    return False

If the list doubles in length, the worst-case number of comparisons roughly doubles, so the algorithm is linear in time.

Indexing into a list by position is typically O(1):

python
def first_item(items):
    return items[0]

The cost does not grow with the list's length, so it is constant time.

Space complexity: growth in memory

Space complexity asks how much extra memory the algorithm allocates while it runs: temporary arrays, recursion stack, hash tables and other supporting structures.

This function builds a new list of doubled values:

python
1def doubled(items):
2    result = []
3    for item in items:
4        result.append(item * 2)
5    return result

It uses O(n) extra space because result grows in proportion to the input.

Compare an in-place update:

python
1def double_in_place(items):
2    for i in range(len(items)):
3        items[i] *= 2
4    return items

This version still takes O(n) time because it touches every element, but uses only O(1) extra space because it modifies the existing list instead of allocating another.

The time-space tradeoff

A fast algorithm does not always use little memory, and a memory-efficient one is not always fast. A hash set, for example, cuts lookup time at the cost of extra memory:

python
1def has_duplicates(items):
2    seen = set()
3    for item in items:
4        if item in seen:
5            return True
6        seen.add(item)
7    return False

This is usually described as O(n) time and O(n) space. The set speeds up membership checks but stores up to n values.

Sorting first may reduce extra space, depending on the implementation, but often costs more time:

python
1def has_duplicates_sorted(items):
2    items = sorted(items)
3    for i in range(1, len(items)):
4        if items[i] == items[i - 1]:
5            return True
6    return False

You usually get around O(n log n) time, and the extra space depends on the sort and language implementation.

Big O for both measures

Both are commonly written in Big O notation, which describes the growth trend rather than exact constants. O(n) time does not mean exactly n operations; it means work grows linearly with input size, up to constant factors.

The same goes for memory. One extra array of size n means O(n) additional space. A handful of variables regardless of input size means O(1) extra space. That is why two algorithms can share a time complexity and differ in space complexity, or the other way around.

Pick the metric that matches your constraint

  • On a tiny embedded device, memory may be the hard limit.
  • In a high-throughput service, runtime may dominate cost.
  • In data processing, both may matter because large inputs stress CPU and RAM together.

A faster algorithm is not better if it blows out memory, and a memory-saving one is not better if response times become unacceptable.

Mistakes in complexity analysis

  • Treating time complexity as stopwatch time. It ignores hardware, constants and implementation details; complexity describes growth.
  • Forgetting the recursion stack. Recursive algorithms often use extra memory even without explicit arrays.
  • Counting the input as extra space. Space complexity usually means auxiliary memory beyond the input.
  • Assuming lower time complexity always performs better. Constant factors and practical constraints still matter.
  • Ignoring the tradeoff. Many algorithms gain speed by spending memory, or save memory by doing more work.

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
Basis Lab
There is a whole course on this

Basis Lab has written “How Long It Takes”, a course that teaches this from the ground up rather than answering the single question. It is a separate product from the same team and the course is free to read.

Read the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms