Foundation · Module 1 · Lesson 3

Space complexity

Input space, auxiliary space, and recursion stacks.

15–25 min readLesson 3 of 123

Lesson 3 of 123

1d streak

Detailed theory

Space complexity measures how memory usage grows with input size. In interviews, “space” usually means auxiliary space, but a complete analysis should say whether input, output, and recursion-stack memory are included.

By the end, you can

  • Separate input space, output space, and auxiliary space.
  • Count recursion-stack frames correctly.
  • Compare in-place and copying approaches.

Vocabulary

Definitions with concrete meaning

Input space

Memory occupied by the data supplied to the algorithm.

Example: An input array of n integers occupies Θ(n) space.

Auxiliary space

Extra working memory allocated by the algorithm, excluding the input.

Example: Two indices and one temporary value use Θ(1) auxiliary space.

Output space

Memory required for the returned result. State whether it is counted, especially when output size is unavoidable.

Example: Generating every subset requires Θ(n2ⁿ) output storage if all subsets are retained.

Call-stack space

Memory for active recursive frames; it depends on maximum recursion depth.

Example: Recursive binary search uses Θ(log n) stack space; the iterative version uses Θ(1).

Rules to apply

  1. 1Declare what your space figure includes; “O(1) auxiliary” is more precise than “O(1) space.”
  2. 2Count simultaneously live allocations, not total bytes ever allocated over time.
  3. 3A loop does not automatically use O(n) space; reused scalar variables remain O(1).
  4. 4A recursive function uses one frame per active level, even if it allocates no explicit collection.
  5. 5In-place usually means O(1) auxiliary space, though the input itself still occupies memory.
  6. 6An output-sensitive algorithm may need space proportional to the answer size.

Worked example 1

Reverse an array: copy versus in-place

Compare building a reversed array with swapping values inside the input.

  1. 1

    Copying approach

    Allocate n output slots and fill them in reverse order: Θ(n) auxiliary space.

  2. 2

    In-place approach

    Keep left, right, and temp; swap until pointers meet: Θ(1) auxiliary space.

  3. 3

    State the trade-off

    Both take Θ(n) time, but the in-place version mutates the input while the copy preserves it.

Space optimization can change semantics. “In-place” is not automatically the better API.

Worked example 2

Count recursive space

Merge sort recursively splits the array and uses a temporary merge buffer.

  1. 1

    Recursion depth

    Balanced splitting creates Θ(log n) active stack frames.

  2. 2

    Merge storage

    Temporary arrays total Θ(n) simultaneously at the dominant merge level.

  3. 3

    Combine live memory

    Θ(n) buffer + Θ(log n) stack = Θ(n) auxiliary space.

Add simultaneously live components, then keep the dominant term.

Comparison table

Time–space examples

The exact result can depend on implementation choices.

ApproachTimeAuxiliary spaceImportant note
Iterative array reverseΘ(n)Θ(1)Mutates input
Copying array reverseΘ(n)Θ(n)Preserves input
Iterative binary searchΘ(log n)Θ(1)No call stack
Recursive binary searchΘ(log n)Θ(log n)One frame per level
Merge sortΘ(n log n)Θ(n)Merge buffer dominates stack
BFSΘ(V + E)Θ(V)Queue may hold a full frontier

Check your understanding

1Does a loop that runs n times use Θ(n) auxiliary space?

Not necessarily. If each iteration reuses the same fixed variables, auxiliary space remains Θ(1).

2Why is recursive binary search not Θ(1) auxiliary space?

Its Θ(log n) active calls each occupy a stack frame.

3Can an algorithm be O(1) auxiliary space but return O(n) data?

Yes, when output storage is reported separately or required by the problem. State the convention clearly.

Cost Model

Complexity & Trade-offs

In-place array reverse
Θ(n)
Θ(1) auxiliary
Two pointers and one temporary value are reused.
Merge sort
Θ(n log n)
Θ(n) auxiliary
The merge buffer dominates the Θ(log n) call stack.
Recursive DFS
Θ(V + E)
Θ(V)
Visited state and a worst-case depth-V call stack are both linear.

When to use it

  • Working under memory limits or on large inputs.
  • Choosing iterative versus recursive implementations.
  • Evaluating in-place mutation, copying, caching, and streaming trade-offs.

Common mistakes

  • Ignoring recursive call frames.
  • Calling an algorithm O(1) space without saying “auxiliary.”
  • Adding allocations that are never live at the same time or forgetting a dominant buffer.

Practice prompts

  • Compare iterative and recursive binary-search space.
  • Analyze DFS space on a chain and on a balanced tree.
  • Explain the output-space cost of generating all permutations.

Mastery check

Can you derive it instead of memorizing it?

State the invariant before looking at code.
Explain why every transition is safe.
Construct a worst-case input.
Name one alternative and its trade-off.