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:
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):
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:
It uses O(n) extra space because result grows in proportion to the input.
Compare an in-place update:
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:
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:
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.
Related reading
- Different decision tree algorithms with comparison of complexity or performance
- Digital camera algorithms
- Dijkstra algorithm for 2D array in Java
- Dijkstra vs. Floyd-Warshall Finding optimal route on all node pairs
- Different latency time when measured through javascript vs developer console
- Dijkstra's Algorithm modification
- Dijkstra with Parallel edges and self-loop
- Dijkstra's algorithm a greedy or dynamic programming algorithm?

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 courseThere 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 courseData 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.