Course contentsShow
← Computer Science
Lesson 305 of 2,8727. Introduction to Complexity AnalysisFree lesson

Constant Space: O(1)

Identifying algorithms that use a fixed amount of memory regardless of input size.

Constant Space: O(1)

You'll learn: How to identify algorithms that use a fixed, predictable amount of memory regardless of how large the input gets.

The Core Idea

Constant space, written as O(1), means an algorithm uses the same amount of extra memory whether it's processing 10 items or 10 million items. The memory usage doesn't grow with input size—it stays fixed.

Think of it like a recipe card. Whether you're cooking for 2 people or 200 people, you still only need one recipe card to follow. The card itself doesn't grow bigger as your dinner party expands.

What Counts as O(1) Space?

An algorithm uses constant space when it only needs:

  • A fixed number of variables (counters, temporary values, pointers)
  • No data structures that grow with input size

For example, swapping two array elements in-place uses three variables: temp, an index i, and an index j. Whether your array has 5 elements or 5 billion, you still only need those three variables.

Common Examples

  • Array reversal in-place: Uses two index variables (left and right) and one temp variable—always 3 variables, regardless of array size
  • Finding maximum element: Uses one variable to track the current maximum as you scan through
  • Simple arithmetic calculations: Adding numbers, computing averages with running totals

What's NOT O(1)?

Creating a new array to store results, building a frequency table, or using recursion (which grows the call stack) all use more than constant space.

Key Takeaway: An algorithm has O(1) space complexity when its memory usage remains fixed and doesn't scale with the input size—only a handful of variables, no matter how big the data gets.