Stop 10 · Data Structures & Algorithms in JavaTopics · 1/11

Big-O Notation

Part of the Java Roadmap

In short

Two programs can both give the right answer, yet one finishes in a blink and the other takes all night on big data. Big-O is how you tell them apart before you even run them, and every interviewer will ask you about it.

Big-O describes how running time or memory grows with input size, for example O(1), O(log n), O(n), O(n log n) and O(n²). It helps you choose an approach before coding.

The jaadu stands at the start of a very long street of identical doors, sweating, while a big glowing arrow points straight to one door.

Think of it like this

Finding a friend's house

If you know the exact flat number, you walk straight in: O(1). If you only know the street, you knock on every door: O(n). If you have a sorted phone directory, you open it in the middle and halve the search each time: O(log n). Same goal, very different effort as the city grows.

Watch it happen

Does this array have a duplicate?

Nested loops: O(n²)

  • Compare every pair of numbers
  • 1,000 items means about 500,000 comparisons
  • 1 lakh items means about 5 billion
  • No extra memory needed

HashSet: O(n)

  • Walk the array once
  • Ask the set 'seen this before?' in O(1) on average
  • 1 lakh items means 1 lakh quick checks
  • Uses O(n) extra memory for the set

Trading a little memory for a much better time complexity is the most common optimisation in interviews.

The big ideas

What you need to know

Growth, not seconds

Big-O ignores constants and hardware. It asks: if the input doubles, roughly how much more work happens?

The common ladder

O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ). Anything above n log n gets painful for large n.

Drop constants and small terms

O(2n + 10) is just O(n). O(n² + n) is O(n²). Only the fastest growing part matters.

Space counts too

Extra arrays, maps and recursion depth all use memory. State both time and space in interviews.

See it in code

Walk through the code

java
1import java.util.HashSet;2import java.util.Set;3 4public class BigODemo {5    // O(n²) time, O(1) extra space6    static boolean hasDuplicateSlow(int[] nums) {7        for (int i = 0; i < nums.length; i++) {8            for (int j = i + 1; j < nums.length; j++) {9                if (nums[i] == nums[j]) return true;10            }11        }12        return false;13    }14 15    // O(n) time, O(n) extra space16    static boolean hasDuplicateFast(int[] nums) {17        Set<Integer> seen = new HashSet<>();18        for (int n : nums) {19            if (!seen.add(n)) return true;  // add returns false if already present20        }21        return false;22    }23 24    // O(1): one step no matter how big the array is25    static int first(int[] nums) {26        return nums[0];27    }28}

1/3A loop inside a loop over the same array: about n × n / 2 comparisons, so O(n²).

Three methods, three growth rates. Count the loops and what happens inside them.

Save yourself the pain

Common mistakes

Oops

Saying a method is O(n) because it has one loop, while list.contains() runs inside it.

Do this instead

contains on an ArrayList is itself O(n), so the method is O(n²). Count the cost of library calls too.

Oops

Calling binary search O(n) because it uses a while loop.

Do this instead

The loop halves the range each time, so it runs about log₂(n) times: O(log n).

Your mission

Label the complexity

~20 min

Quick check

A method loops over n items, and inside the loop does a binary search on a sorted array of n items. What is the time complexity?

Go deeper

  1. 1Work out the complexity of 5 methods you've already written.
  2. 2Learn the complexity of common ArrayList, HashMap and TreeMap operations.
  3. 3Compare a brute-force O(n²) solution with an O(n) HashMap solution.

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