What Is Time Complexity

· Category: Tech Fundamentals

Short answer

Time complexity describes how the runtime of an algorithm grows relative to its input size. It abstracts away hardware differences to compare algorithms.

How it works

We count the number of fundamental operations. A single loop over n items is O(n). Nested loops are often O(n^2). Binary search is O(log n) because it halves the search space each step.

Example

Searching an unsorted array requires checking each element: O(n). Using a hash map reduces this to O(1) on average.

Why it matters

Time complexity predicts scalability. An O(n^2) algorithm may work for a thousand items but fail for a million.