<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. DSA
  3. Foundations
All lessons in this course

Lesson 4 of 50 11 minBeginner

Memory cost: space complexity

Measure how much extra memory an algorithm needs, spot hidden memory such as recursion and copies, and trade memory for speed on purpose.

Time is not the only cost. An algorithm also needs memory, and memory runs out too: an OutOfMemoryException, a StackOverflowException, or simply an app that slows down because the garbage collector is busy. Space complexity uses the same Big-O language to describe how memory use grows with n.

We usually count auxiliary (extra) space: the memory the algorithm needs on top of the input it was given. Summing an array needs a couple of variables (O(1) extra), even though the array itself holds n numbers.

Interactive

How much extra memory?

Pick an algorithm and grow the input. Grey cells are the input; coloured cells are the extra memory the algorithm needs.

Space.cs
int Sum(int[] a){    int total = 0;    for (int i = 0; i < a.Length; i++)        total += a[i];    return total;}
Input (not counted)
Extra variables
totali
Extra array cells: 0
Stack frames at the deepest point: 1
the one call
Extra space O(1). Two variables, whatever the size of the array. The input itself is not counted as extra space.

What uses memory?

SourceExampleExtra space
A few local variablesint total, i;O(1)
A new array or collection sized by nnew int[n], new List<T>(items)O(n)
Copies made by LINQ.ToList(), .ToArray(), .OrderBy(...)O(n)
String buildingEach s + x creates a new stringup to O(n) per step
RecursionEach call waiting on the stackO(depth)
A table of pairsnew bool[n, n]O(n²)

Recursion uses hidden memory

Every method call that has not finished keeps a stack frame with its parameters and local variables. A recursive method that goes n calls deep uses O(n) stack space even if it creates no arrays. The default stack for a .NET thread is about 1 MB, so very deep recursion crashes with StackOverflowException, which cannot be caught.

StackDepth.cs
// O(n) extra space: n + 1 frames alive at the deepest pointstatic long SumTo(int n) => n == 0 ? 0 : n + SumTo(n - 1); // O(1) extra space: same answer with a loopstatic long SumToLoop(int n){    long total = 0;    for (int i = 1; i <= n; i++) total += i;    return total;} Console.WriteLine(SumToLoop(1_000_000));// SumTo(1_000_000) would most likely crash with StackOverflowException
Output
500000500000

Trading memory for time

Many fast algorithms are fast because they use extra memory. The duplicate check from the last lesson went from O(n²) time to O(n) time by spending O(n) memory on a HashSet. Caching results (memoisation, next lesson) is the same deal. This trade-off is usually worth it, because memory is cheap and waiting is expensive, but it is a choice, not a free lunch.

ApproachTimeExtra space
Compare every pairO(n²)O(1)
Sort first, then compare neighboursO(n log n)O(1) to O(n) depending on the sort
Remember what you have seen in a HashSetO(n)O(n)

In-place algorithms

An in-place algorithm changes the input directly and needs only O(1) extra memory, like reversing an array by swapping from both ends. In-place code saves memory and is often faster (fewer allocations, better use of the CPU cache), but it modifies the caller’s data. If the caller still needs the original, make a copy or document the behaviour clearly.

How big is “n” in bytes?

ItemSize in .NET (64-bit)1 million of them
bool, byte1 byte1 MB
int, float4 bytes4 MB
long, double8 bytes8 MB
A reference to an object8 bytes8 MB (plus the objects)
A small class object24+ bytes (header + fields)24+ MB
Point3D struct of 3 doubles24 bytes24 MB
Quick check

What is the extra space of int[] Doubled(int[] a) => a.Select(x => x * 2).ToArray();?

Quick check

A recursive method calls itself with n - 1 until n == 0 and uses only a few local variables. What is its extra space?

Practice

A sorted array may contain repeated values. Remove the repeats in place so the first k slots hold each value once, and return k. Use O(1) extra space. Example: { 1, 1, 2, 3, 3, 3, 7 } → k = 4 and the array starts 1, 2, 3, 7.

Show a hint

Keep a “write” index. Walk through the array with a “read” index; when the value differs from the last one written, copy it to the write position and move the write index forward.

Show the solution
Solution.cs
static int RemoveDuplicates(int[] a){    if (a.Length == 0) return 0;    int write = 1;                          // a[0] is always kept    for (int read = 1; read < a.Length; read++)    {        if (a[read] != a[write - 1])        // a new value            a[write++] = a[read];    }    return write;} int[] data = { 1, 1, 2, 3, 3, 3, 7 };int k = RemoveDuplicates(data);Console.WriteLine($"{k}: {string.Join(", ", data[..k])}");
Output
4: 1, 2, 3, 7

Remember

  • Space complexity describes how extra memory grows with n, in Big-O.
  • New arrays, ToList()/ToArray(), string building and recursion depth all use memory.
  • Fast algorithms often trade memory for time (HashSet, caching); make that choice consciously.
  • In-place algorithms use O(1) extra space but change the input.
0 of 50 lessons completed