Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Time complexity

Временная сложность

Describes how the amount of work an algorithm does changes as its input grows. It usually counts operations rather than seconds on a particular machine.

What it measures

Time complexity tells you how an algorithm scales. Say you want to find a particular ID in a list. Scanning the list might require checking every entry, so doubling its length doubles the maximum number of checks. If the IDs are already sorted in an array, binary search can discard about half of the remaining candidates at each step. Doubling the array length then adds at most one more check to the worst case.

How to work it out

Start by defining the input size. For an array, n usually means the number of elements. If an algorithm scans two arrays of unrelated lengths n and m, keep both variables: the work is Θ(n + m) if the algorithm does a fixed amount of work per element. Writing Θ(n) would hide the cost of scanning the second array.

Next, count how often the relevant operations run. Under the usual assumptions, reading an array slot by index costs constant time; comparing two strings does not necessarily do so, because the comparison may need to inspect many characters. Big O gives an asymptotic upper bound on growth. For example, 4n + 12 is O(n), and more precisely Θ(n): for large n, it is bounded both above and below by constant multiples of n.

Example: checking for a duplicate

def has_duplicate(ids):
    for i in range(len(ids)):
        for j in range(i + 1, len(ids)):
            if ids[i] == ids[j]:
                return True
    return False

Assume the IDs are fixed-width integers, so each comparison takes constant time. If there are no duplicates, the function checks n(n - 1) / 2 pairs: the first element is compared with n - 1 others, the second with n - 2, and so on. The worst-case time is Θ(n²). If the first two IDs match, the function exits after one comparison, giving a best-case time of Θ(1). An average-case claim would need assumptions about how the inputs are distributed.

Using the result

A set can replace the pairwise search: check each ID against the set of previously seen IDs, then add it if it is new. With expected constant-time lookups and expected amortized constant-time insertions, a full pass takes expected Θ(n) time but uses O(n) extra space. Complexity helps you see that trade-off; a benchmark tells you whether it pays off at the input sizes you actually see.

What interviewers look for

Give the bound and explain what produces it. Name the input size, account for early exits, and state any assumptions about helper operations. Avoid treating Big O as a synonym for worst case: worst case says which inputs you measure, while Big O says how the resulting cost is bounded.