By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
You need a way to compare algorithms that works regardless of the machine running them or the programming language used. Big-O notation gives you that language. It describes how an algorithm's runtime grows as the input size increases, which is what actually matters in practice.
Big-O strips away hardware differences, language overhead, and constant factors. An O(n) algorithm on a fast machine is still fundamentally better than an O(n²) algorithm on that same machine when n gets large. Every serious coding interview will ask you to analyze complexity, so this is non-negotiable.
Think of these as tiers from best to worst. At the top: O(1) constant time covers array index lookups and hash map gets. O(log n) logarithmic time appears in binary search and balanced tree operations. O(n) linear time is a single pass through n elements.
O(n log n) linearithmic time is what you get from merge sort and heap sort. O(n²) quadratic time comes from nested loops—avoid this for inputs larger than about 10,000 elements. O(2ⁿ) exponential time shows up in naive recursive solutions like Fibonacci and should be avoided entirely unless the problem is small.
The method is straightforward: count loops and how they nest. A single loop is O(n). Two nested loops checking every pair is O(n²). A loop that halves the search space each time is O(log n).
# O(1) — one operation, no matter how large nums is
def get_first(nums): return nums[0]
# O(n) — one pass through the list
def find_max(nums):
best = nums[0]
for n in nums:
if n > best: best = n
return best
# O(n²) — nested loops, every pair checked
def has_duplicate_slow(nums):
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] == nums[j]: return True
return False
# O(n) — hash set reduces inner loop to O(1)
def has_duplicate_fast(nums):
seen = set()
for n in nums:
if n in seen: return True
seen.add(n)
return FalseLook at has_duplicate_slow first. The nested loops examine every pair of elements, so that's O(n²). Now look at has_duplicate_fast. It trades memory for speed by using a hash set. Instead of an inner loop, checking membership in the set is O(1). The outer loop still runs n times, so the total is O(n).
Space complexity measures the extra memory an algorithm uses beyond storing the input itself. The hash set in has_duplicate_fast uses O(n) extra space—you're storing up to n elements in memory.
This is a classic time-space tradeoff. You paid memory to save time. Sometimes that's the right call. Sometimes it isn't. You need to know both numbers to make that decision.
Always state time AND space complexity. Saying 'O(n) time, O(1) space' shows you think about both axes.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.