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

O(1): Constant Time

Exploring algorithms whose runtime doesn't depend on input size, like array access or variable assignment.

O(1): Constant Time

What you'll learn: You'll understand what O(1) means and recognize operations that take the same amount of time regardless of data size.

The Core Idea

Some operations in programming take the same amount of time whether you're working with 10 items or 10 million items. We call these O(1) or constant time operations.

Think of it like flipping a light switch. It doesn't matter if your house has 3 rooms or 300 rooms—flipping the switch takes exactly one action. The "input size" (number of rooms) doesn't affect the operation time.

Common O(1) Operations

When you analyze an algorithm and count primitive operations, certain actions are always O(1):

  • Array access by index: array[5] jumps directly to position 5, no matter how long the array is
  • Variable assignment: x = 10 takes one operation
  • Arithmetic operations: a + b or x * 2 are single operations
  • Comparison operations: if (x > y) evaluates in constant time
  • Returning a value: return result

Why It Matters

In your runtime analysis, when you see code that doesn't loop or recurse based on input size, you're likely looking at O(1). For example:

function getFirstElement(array):
    return array[0]

Whether the array has 10 elements or 10 billion, accessing array[0] is one primitive operation. No loops. No input-size dependency. That's O(1).

Key Takeaway: O(1) means constant time—the algorithm's runtime stays the same regardless of input size, like accessing an array element by index or performing a simple assignment.