Sign in

Algorithms

Understanding time complexity and Big O notation from the ground up

Learn how to quantify the execution efficiency of your code across varying input sizes using Big O notation, asymptotic analysis, and real-world Python examples.

Plain words. One step at a time. Nothing here assumes you already know the idea.

The idea

Time complexity is a mathematical characterization of how the runtime of an algorithm increases as the size of its input grows. Big O notation expresses this relationship by defining an upper bound on growth, abstracting away hardware differences and low-level constant factors.

See it

Imagine delivering parcels around a city.

1 of 6

You will need. Basic familiarity with Python syntax · Understanding variables, loops, and conditional statements

One pass

Step 1 of 3

When evaluating how fast an algorithm runs, measuring wall-clock time in seconds is unreliable. Running the same code on a slow phone produces a different duration than running it on a supercomputer. Background tasks, operating system scheduling, and hardware architectures also distort measurements. Instead of tracking elapsed seconds, computer scientists count the number of fundamental computational steps executed by the code relative to the size of the input.

Smallest version

Comparing constant and linear time algorithms on an input list

def get_first_element(numbers):
    # Constant time: O(1)
    # Always executes exactly one index lookup, regardless of list size.
    if not numbers:
        return None
    return numbers[0]

def calculate_sum(numbers):
    # Linear time: O(N)
    # The loop runs once for every element in the list.
    total = 0
    for value in numbers:
        total += value
    return total

# Demonstration
sample = [10, 20, 30]
print(get_first_element(sample))  # 1 step
print(calculate_sum(sample))      # 3 steps

Keep these

  • Time complexity measures how operation counts scale with input size, not wall-clock seconds.
  • O(1) represents constant time, meaning runtime does not depend on input size.
  • O(N) represents linear time, where running time grows in direct proportion to input size.

Try one

Analyze an accumulator loop with early exit

easy

Start the five-step lesson

Also on MonksBit