Big-O Notation & Time Complexity, Simply Explained

    Atul Kabra4 min readUpdated

    Big-O notation describes how the running time (or memory) of an algorithm grows as its input grows. It does not measure seconds on your laptop. It answers a sharper question: if you double the input, does the work double, stay flat, or explode? That single idea is what lets you compare two solutions before you ever run them.

    Why we measure growth, not seconds

    A stopwatch lies. The same code runs faster on a newer machine, slower when the cache is cold, and differently depending on the compiler. Big-O ignores all of that. It strips away constants and small terms and keeps only the part that dominates as the input gets large.

    So if an algorithm does 3n + 50 operations, we call it O(n). The 3 and the 50 vanish, because when n is a million, they stop mattering. We care about the shape of the growth curve.

    The common growth rates

    Here are the rates you will meet most often, from fastest to slowest:

    • O(1) — constant. The work never changes with input size. Looking up an array element by index is O(1).
    • O(log n) — logarithmic. The work grows very slowly; doubling the input adds just one more step. Binary search is O(log n).
    • O(n) — linear. Double the input, double the work. A single loop over an array is O(n).
    • O(n log n) — linearithmic. Slightly worse than linear. Good sorting algorithms like merge sort sit here.
    • O(n2) — quadratic. A loop inside a loop. Double the input and the work goes up four times.
    • O(2^n) — exponential. Work doubles with each extra element. These get unusable fast.

    A worked example

    Consider checking whether an array contains a duplicate using two nested loops:

    // Returns true if any value appears twice.
    // Outer loop runs n times; for each, the inner loop runs up to n times.
    // That is roughly n * n comparisons -> O(n^2) time.
    boolean hasDuplicate(int[] arr) {
        for (int i = 0; i < arr.length; i++) {        // runs n times
            for (int j = i + 1; j < arr.length; j++) { // runs ~n times each
                if (arr[i] == arr[j]) {
                    return true;  // found a duplicate
                }
            }
        }
        return false; // no duplicate found
    }
    

    The nested loops make this O(n2). With a hash set you could solve the same problem in O(n) time, trading some extra memory for speed. That trade-off is exactly the kind of decision Big-O helps you make.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Worst, best, and average case

    Big-O usually refers to the worst case — the most work the algorithm could possibly do. Linear search is O(n) in the worst case (the item is last, or missing), even though it might get lucky and find the target on the first try. Reporting the worst case keeps you honest: you plan for the slowest path, not the fluke.

    Space complexity

    The same notation applies to memory. An algorithm that builds a copy of the input array uses O(n) extra space. One that uses a handful of fixed variables uses O(1) space. Often you trade space for time, like the hash-set duplicate check above.

    Common mistakes

    • Counting constants. Writing O(2n) or O(n + 5) misses the point. Drop constants and lower-order terms: it is just O(n).
    • Confusing the number of loops with the exponent. Two loops one after another is O(n) + O(n) = O(n), not O(n2). Only nested loops multiply.
    • Assuming O(n log n) beats O(n2) for tiny inputs. Big-O describes large inputs. For ten elements, constants can flip the result. It is a tool for scale, not micro-tuning.
    • Forgetting hidden costs. A loop that calls list.contains() inside it may secretly be O(n) per call, making the whole thing O(n2). Always check what library calls cost.
    • Ignoring space. Fast code that allocates a huge array can still crash on real data. Track both axes.

    FAQ

    Is O(1) always faster than O(n)? For large inputs, yes, that is the guarantee. For tiny inputs the constant factors can make an O(n) routine win in raw seconds, but the growth shape still favours O(1) as data scales.

    Do I need heavy maths to use Big-O? No. You need to spot loops, recursion, and how the work scales when the input grows. The intuition matters more than formal proofs for everyday coding.

    Keep learning

    Big-O is the lens you will use to judge every data structure and algorithm that follows. Pair it with hands-on practice: read our Linear vs Binary Search guide to see O(n) and O(log n) side by side, browse the full DSA learning hub, or follow the beginner's DSA roadmap for a structured path.

    Want guided practice with feedback from instructors in Jalgaon? Join the waitlist for our Data Structures & Algorithms course and work through complexity analysis on real problems.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides