Skip to content
TeachTrails
DSA for BeginnersPart 1 of 23 · 0/23 done
  1. Part 1: Big O Notation Graph & Examples, Explained Simply
  2. Part 2: Binary Search Algorithm Explained (Time Complexity + Code)
  3. Part 3: Recursion Explained: How It Works (With Call Stack Visual)
  4. Part 4: Sorting Algorithms Explained: Visualization & Time Complexity
  5. Part 5: Linked List in Data Structure: Types, Operations & Code
  6. Part 6: Stack vs Queue: Difference Explained (With Animation)
  7. Part 7: Hashing in Data Structure: Hash Tables & Hash Maps Explained
  8. Part 8: Binary Search Tree (BST): Insert, Search & Tree Traversals
  9. Part 9: BFS vs DFS: Graph Traversal Explained (With Animation)
  10. Part 10: Dynamic Programming for Beginners: Memoization vs Tabulation
  11. Part 11: Two Pointers Technique: The Easiest Interview Pattern
  12. Part 12: Sliding Window Technique Explained Simply (With Examples)
  13. Part 13: Prefix Sum Explained: Range Sums and Subarray Sum = K
  14. Part 14: Binary Search on Answer: Koko Eating Bananas Made Easy
  15. Part 15: Fast and Slow Pointers: Detect a Cycle in a Linked List
  16. Part 16: Merge Intervals Explained: Sort, Then Glue the Overlaps
  17. Part 17: Monotonic Stack Explained: Next Greater Element Made Easy
  18. Part 18: Heap and Top-K Problems Explained: Kth Largest Made Easy
  19. Part 19: Binary Tree Patterns: DFS and BFS for Coding Interviews
  20. Part 20: Grid BFS and Topological Sort: Islands and Course Schedule
  21. Part 21: Backtracking Explained: Subsets, Permutations and N-Queens
  22. Part 22: Dynamic Programming Patterns: Knapsack, LIS and LCS Explained
  23. Part 23: Which DSA Pattern? 30 Interview Problems to Test Yourself
DSAAlgorithms

Big O Notation Graph & Examples, Explained Simply

Big O notation explained for beginners: what O(1), O(log n), O(n) and O(n²) mean, with a growth graph, code examples and an easy memory trick.

Sudipta Biswas, AI Engineer

10 min read

Beginner friendly
input size (n) →steps (time) →O(2ⁿ)horribleO(n²)badO(n log n)fairO(n)goodO(log n)greatO(1)excellent
How fast does the work grow?

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

input size (n) →steps (time) →O(2ⁿ)horribleO(n²)badO(n log n)fairO(n)goodO(log n)greatO(1)excellent
The Big O graph. Flatter lines scale better; steep curves get slow fast.
Big O notation graph comparing O(1), O(log n), O(n), O(n log n), O(n²) and O(2ⁿ) as input size grows
Big O notation graph (save it as a cheat sheet): O(1) and O(log n) stay flat, O(n²) and O(2ⁿ) shoot up.

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.

816321001k10k1M
O(1)1 steps · instant
O(log n)5 steps · instant
O(n)32 steps · instant
O(n log n)160 steps · instant
O(n²)1,024 steps · 1 µs
O(2ⁿ)4.3 × 10^9 steps · 4.3 s

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

  1. 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).
  2. Drop the constants. 2n, n + 10 and n/2 are all just O(n). Big O cares about the shape, not the exact count.
  3. Keep only the biggest term. n² + n becomes O(n²), because for large n the n² part completely dominates.
Big O examples: O(1), O(n) and O(n²)
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 snippets
  1. def first(items):
        return items[0]
  2. for x in items:
        print(x)
  3. for a in items:
        for b in items:
            print(a, b)
  4. while n > 1:
        n = n // 2
  5. for 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”.

  1. P

    Picture it

    Your brain remembers scenes far better than definitions.

    O(1)greet the 1st guestO(log n)halve the guest listto find one nameO(n)host shakes everyhand onceO(n²)everyone shakeseveryone's hand

    ← 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²).

  2. 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)
  3. 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.
  4. H

    Hit-recall it

    Test yourself now, then again on the day each card comes back.

    Card 1 / 8 · new0 mastered · 0 practised

    Say 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 list in 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 in the course, see it in action with binary search, the classic O(log n) algorithm. Later you'll compare O(n²) and O(n log n) for real in the sorting algorithms visualizer.

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. 1. Which of these grows the slowest as n gets large?

  2. 2. An algorithm does 3n + 50 steps. What is its Big O?

  3. 3. A loop inside another loop over the same list is usually…

  4. 4. Using the anchor “1 Lion Naps, Nine Lions Snore Endlessly”, what does “Snore” stand for?

DSA for Beginners

Part 1 of 23: Big O Notation Graph & Examples, Explained Simply