Skip to content
AI-grafen
DAI developerComputer science· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

Time complexity and big-O notation

Be able to judge how an algorithm's time grows with the input and tell O(n) from O(n²).

Prerequisites

Intuition

Big-O describes how the running time grows as the input grows — not how many seconds something takes on your particular computer.

Big-OName1 000 elements1 000 000 elements
O(1)constant11
O(log n)logarithmic1020
O(n)linear1 0001 000 000
O(n log n)linearithmic10 00020 000 000
O(n²)quadratic1 000 00010¹² (too slow)

Constants and smaller terms are dropped: 3n + 50 is O(n), n² + 1000n is O(n²).

Code

def has_duplicates_naive(items):      # O(n²) — two nested loops
    for i in range(len(items)):
        for j in range(i + 1, len(items)):
            if items[i] == items[j]:
                return True
    return False

def has_duplicates_fast(items):       # O(n) — a set checks in constant time
    seen = set()
    for x in items:
        if x in seen:
            return True
        seen.add(x)
    return False

With 10 000 elements: the first makes about 50 million comparisons, the second 10 000. On a million elements the first is entirely impossible while the second takes fractions of a second.

The counting rule: a loop over n is O(n). A loop inside a loop is O(n²). Halving per step is O(log n). Sorting is O(n log n).

In AI: attention is O(T²) in the sequence length — that is why long contexts are expensive, and why alternatives are being researched.

Mastery means

  • States the big-O of a simple loop structure
  • Explains why O(n²) becomes unmanageable at large n

Sign in to do the exercises and build your mastery up.

Sources

All the sources and licences