Big O notation is the first big hurdle in every data structures and algorithms (DSA) course, and it shows up in almost every coding interview. The good news: the idea behind it is simple. Big O answers one question: when the input gets bigger, how fast does the work grow?
In this guide you'll get the plain-English meaning, a Big O graph, code examples in Python, Java and C++, a live “growth race”, and a memory trick so the order of complexities sticks for good.
What is Big O notation?
Imagine two apps that both search a contact list. With 10 contacts, both feel instant. With 10 million contacts, one still answers instantly and the other freezes. Big O is the tool that predicts this before you run anything.
It doesn't measure seconds, because seconds depend on your laptop. Instead it describes the growth rate: how the number of steps changes as the input size, called n, gets larger. That's why it's also called time complexity.
The Big O graph: common complexities
O(1): constant time
The work is the same no matter how big the input is. Reading items[0] from an array takes one step whether the array has 10 or 10 million elements.
O(log n): logarithmic time
The problem is cut in half at every step. Looking up a word in a dictionary by opening the middle, then the middle of the right half, and so on is O(log n). It's how binary search works, and it's incredibly fast: about 20 steps for a million items.
O(n): linear time
The work grows in step with the input. Checking every item in a list once is O(n).
O(n log n): linearithmic time
The sweet spot for sorting. Efficient sorting algorithms like merge sort split the data in halves (log n levels) and do n work at each level.
O(n²): quadratic time
Usually a loop inside a loop: for each item, look at every other item. Fine for 100 items, painful for 100,000. Simple sorts like bubble sort are O(n²).
O(2ⁿ): exponential time
Every extra input item doubles the work. Trying every possible combination of n items grows like this. Even n = 60 is out of reach for any computer.
🏁 The growth race
Change the input size and see how many steps each Big O needs.
Times assume a computer doing about one billion simple steps per second. Bars use a log scale so the giants still fit on screen.
How to calculate Big O: 3 simple rules
- Count the loops over the input. No loop → O(1). One loop → O(n). A loop inside a loop → O(n²). Halving each time → O(log n).
- Drop the constants. 2n, n + 10 and n/2 are all just O(n). Big O cares about the shape, not the exact count.
- Keep only the biggest term. n² + n becomes O(n²), because for large n the n² part completely dominates.
def get_first(items): # O(1): one step, any size
return items[0]
def contains(items, target): # O(n): may check every item
for x in items:
if x == target:
return True
return False
def has_duplicate(items): # O(n²): compares every pair
for i in range(len(items)):
for j in range(i + 1, len(items)):
if items[i] == items[j]:
return True
return False🔍 Spot the Big O
5 snippetsdef first(items): return items[0]for x in items: print(x)for a in items: for b in items: print(a, b)while n > 1: n = n // 2for x in items: print(x) for y in items: print(y)
🧠 PATH memory card
Remember the order of Big O complexities for good
Picture it · Anchor it · Trace it · Hit-recall it. Four quick steps that turn “I read it once” into “I can explain it in an exam”.
- P
Picture it
Your brain remembers scenes far better than definitions.
← swipe to see the whole picture →
You're hosting a party. Greeting the first guest is O(1). Finding a name by halving the guest list is O(log n). Shaking every guest's hand is O(n). Everyone shaking everyone's hand is O(n²).
- A
Anchor it
A silly sentence you can't forget, where every word maps to the concept.
“1 Lion Naps, Nine Lions Snore Endlessly”
- 1→O(1): constant
- Lion→O(log n): logarithmic
- Naps→O(n): linear
- Nine Lions→O(n log n): n × log n
- Snore→O(n²): square
- Endlessly→O(2ⁿ): exponential (never-ending)
- T
Trace it
Run it by hand on a tiny example once. Doing beats reading.
Look at any code and ask three questions: Is there a loop? (O(n)) Is there a loop inside the loop? (O(n²)) Does the problem halve each time? (O(log n)). Try it on the five snippets in “Spot the Big O” above, out loud. - H
Hit-recall it
Test yourself now, then again on the day each card comes back.
Card 1 / 8 · new0 mastered · 0 practisedSay the answer out loud first, then flip and be honest.
Common Big O mistakes students make
- Counting two separate loops as O(n²). One loop after another is O(n) + O(n) = O(n). Only nested loops multiply.
- Keeping constants. Writing O(2n) isn't wrong, but the convention is O(n).
- Forgetting hidden loops. Built-in operations like searching a list (
x in listin Python) loop internally and cost O(n). - Mixing up time and space. A fast algorithm might use extra memory; analyse both.
What to learn next
Big O is the language you'll use for every other DSA topic. Next, see it in action with binary search (the classic O(log n) algorithm), compare O(n²) vs O(n log n) in our sorting algorithms visualizer, and learn the two most common data structures in stack vs queue.
Key takeaways
- Big O describes how an algorithm's work grows as the input size n grows.
- Best to worst: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ). Remember “1 Lion Naps, Nine Lions Snore Endlessly.”
- One loop is O(n), a nested loop is O(n²), halving each step is O(log n).
- Drop constants and keep only the biggest term.
- Analyse both time and space complexity, especially in interviews.
Frequently asked questions
What is Big O notation in simple words?
Big O notation describes how the running time (or memory use) of an algorithm grows as the input gets bigger. It ignores exact seconds and small details and focuses on the growth pattern, such as constant, linear or quadratic.
What is the difference between O(n) and O(log n)?
O(n) means the work grows in step with the input: double the data, double the work. O(log n) means the work grows very slowly, because each step cuts the problem in half. For a million items, O(n) can take a million steps while O(log n) takes about 20.
Why do we drop constants in Big O?
Because Big O is about the shape of growth for large inputs. An algorithm that does 2n or n + 100 steps still grows linearly, so both are written O(n). Constants matter in practice, but they don't change the growth class.
Is Big O the worst case?
Big O is most often used to describe the worst case, which is the safest guarantee to give. Strictly speaking it is an upper bound on growth. You will also see best-case and average-case analysis, and related notations like Big Omega (Ω) and Big Theta (Θ).
What is space complexity?
Space complexity uses the same Big O idea for memory instead of time: how much extra memory an algorithm needs as the input grows. For example, copying a list needs O(n) extra space, while swapping two variables needs O(1).
🧠 Quick check: Big O notation
1. Which of these grows the slowest as n gets large?
2. An algorithm does 3n + 50 steps. What is its Big O?
3. A loop inside another loop over the same list is usually…
4. Using the anchor “1 Lion Naps, Nine Lions Snore Endlessly”, what does “Snore” stand for?