Stop 12 · Data Structures & AlgorithmsTopics · 1/11

Big-O Notation

Part of the Python Roadmap

In short

Two solutions can both give the right answer, yet one finishes instantly and the other takes hours on a big input. Big-O is the shared language for saying how code slows down as data grows, and interviewers ask about it in almost every coding round.

Big-O describes how an algorithm's time or memory grows as the input grows, such as O(1), O(log n), O(n) and O(n²). It lets you compare approaches before you write code and is asked in almost every coding round.

The jaadu stands on a chair at a crowded, colourful wedding hall, scanning the guests with binoculars while holding a seating chart in the other hand.

Think of it like this

Finding a friend at a wedding

At a big Punjabi wedding, you can walk up to every guest one by one (time grows with the crowd: O(n)), or check the seating chart that tells you the table directly (same time however big the wedding: O(1)). Comparing every guest with every other guest to find matching outfits? That's O(n²), and it gets painful fast.

Watch it happen

Does a list of phone numbers have a duplicate?

Nested loops: O(n²)

  • Compare every number with every later number
  • Double the list, and the work roughly quadruples
  • Uses almost no extra memory

Set lookups: O(n)

  • Walk the list once, remembering what you've seen
  • Double the list, and the work roughly doubles
  • Uses O(n) extra memory for the set

Trading a little memory for a big speed-up is the most common optimisation in coding interviews.

The big ideas

What you need to know

Growth, not seconds

Big-O ignores your laptop's speed and constant factors. It describes how work grows as n grows.

The common ladder

O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ). Each step up gets slow much sooner.

Keep the biggest term

O(n² + n + 5) is just O(n²). For large inputs, the biggest term dominates everything else.

Space counts too

Extra memory has a Big-O as well. A set of seen items is O(n) space.

See it in code

Walk through the code

python
1def has_duplicate_slow(phones: list[str]) -> bool:2    # O(n²) time, O(1) extra space3    for i in range(len(phones)):4        for j in range(i + 1, len(phones)):5            if phones[i] == phones[j]:6                return True7    return False8 9 10def has_duplicate_fast(phones: list[str]) -> bool:11    # O(n) time, O(n) extra space12    seen: set[str] = set()13    for phone in phones:14        if phone in seen:15            return True16        seen.add(phone)17    return False18 19 20numbers = ["98450 11111", "99000 22222", "98450 11111"]21print(has_duplicate_slow(numbers), has_duplicate_fast(numbers))  # True True

1/3A loop inside a loop over the same list. About n × n / 2 comparisons: that's O(n²).

Same answer, very different growth. The fast version also stops early the moment it finds a repeat.

Save yourself the pain

Common mistakes

Oops

Thinking x in my_list is instant.

Do this instead

Searching a list is O(n). Searching a set or dict key is O(1) on average. Inside a loop, this difference is huge.

Oops

Calling list.pop(0) or list.insert(0, x) in a loop.

Do this instead

Both are O(n) because every other item shifts. Use collections.deque for work at the front.

Oops

Saying an algorithm is O(2n).

Do this instead

Drop the constants: O(2n) is O(n). Interviewers expect the simplified form.

Your mission

Label the complexity

~20 min

Quick check

What is the time complexity of a loop over n items where each step does a set lookup?

Go deeper

  1. 1Write down the Big-O of common list, dict and set operations in Python.
  2. 2Compare a nested-loop solution with a single-pass solution for the same problem.
  3. 3State the time and space complexity after every practice problem you solve.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge