Home · Blog · Technical and coding
Big O notation for interviews, explained simply
Almost every coding interview ends with "What's the time and space complexity?" You don't need a maths degree to answer. This guide explains big O in plain words, shows the complexities you'll meet most, and how to work out and explain the complexity of your own code.

In short
- Big O describes how time or memory grows as the input grows, ignoring constants.
- Learn the common ones - O(1), O(log n), O(n), O(n log n), O(n squared), O(2 to the n).
- Work it out from your loops - sequential steps add, nested loops multiply.
- Always give both time and space, and say which variable n is.
Practise this free: a mock interview for Coding round (algorithms), scored, with a stronger answer. 3 a month, no card.
Start a mock interviewBig O notation answers one question: if the input gets bigger, how much slower does my code get, and how much more memory does it use? That’s it. Interviewers ask about it because it shows whether you understand the cost of your solution, which matters at work when inputs are large.
What big O means
Big O describes growth, not exact time. It ignores constant factors and small terms:
- A loop over n items that does three things per item is O(3n), written O(n).
- A nested loop plus a single loop is O(n squared + n), written O(n squared), because for large n the nested loop dominates.
Usually you describe the worst case. When average and worst differ a lot, as with hash maps or quicksort, say both.
The common complexities
From fastest to slowest growth:
- O(1), constant: the same work regardless of size. Reading an array element by index, a hash map lookup on average.
- O(log n), logarithmic: the problem halves each step. Binary search, balanced tree lookups.
- O(n), linear: one pass through the input. Finding the maximum, counting with a hash map.
- O(n log n): efficient sorting, such as merge sort, and many “sort first” solutions.
- O(n squared), quadratic: nested loops over the input. Comparing every pair.
- O(2 to the n), exponential: trying every subset. Naive recursive solutions without memoisation.
- O(n!), factorial: trying every ordering, such as all permutations.
A rough guide to what fits in an interview problem’s limits: with a million elements, aim for O(n) or O(n log n); with a few thousand, O(n squared) may be fine; exponential only works for very small inputs, around 20 to 25.
How to work out the complexity of your code
- Sequential steps add. A loop, then another loop: O(n + n) = O(n).
- Nested loops multiply. A loop inside a loop over the same input: O(n squared).
- Halving gives log n. If each step cuts the remaining work in half, it’s O(log n).
- Recursion: count the calls and the work per call. A function calling itself twice per level, n levels deep, makes about 2 to the n calls.
- Built-in operations count. Sorting is O(n log n); searching a list with “in” is O(n); slicing a list copies it.
Watch out for hidden costs: string concatenation in a loop, list.remove, or checking membership in a list instead of a set. The Python documentation’s page on data structures shows which operations are cheap.
Average, worst and amortised cases
Some operations have different costs depending on the situation, and interviewers like it when you know the difference:
- Worst case is the default in interviews. Quicksort is O(n log n) on average but O(n squared) in the worst case.
- Average case matters for hash maps: lookups are O(1) on average, but many collisions can make them slower. Saying “O(1) on average” is the precise answer.
- Amortised cost describes operations that are usually cheap but occasionally expensive. Appending to a dynamic array is O(1) amortised: most appends are instant, and once in a while the array is copied to a bigger one, but averaged over many appends the cost per append stays constant.
You don’t need formal proofs. One sentence showing you know which case you’re describing is enough: “Appending is O(1) amortised, so building the list is O(n) overall.”
Space complexity
Count the extra memory your algorithm uses, not the input:
- A few variables: O(1).
- A hash map or array that grows with the input: O(n).
- Recursion uses stack space: a recursion n levels deep uses O(n) space, even without other data structures.
How to explain it in the interview
Be specific about what n is, give time and space, and say why.
“Time is O(n log n), where n is the number of intervals: sorting dominates, and the merge pass after it is O(n). Space is O(n) for the output list; apart from that I only use a couple of variables. If the input were already sorted, it would be O(n).”
If there are two inputs, use two variables: “O(n + m), where n is the length of the first list and m the second.”
Improving the complexity
Interviewers often follow up with “Can you do better?” The usual moves are: replace a repeated search with a hash map, sort once so you can use two pointers or binary search, keep a running value instead of recomputing it, or store results you’ve already computed (memoisation). Say which move you’re considering and what it would cost in memory before you start rewriting.
Common mistakes
- Forgetting the sort. Sorting first makes the whole solution at least O(n log n).
- Calling a hidden O(n) operation inside a loop, making the solution O(n squared) without noticing.
- Ignoring recursion space.
- Only giving time complexity. Interviewers usually want both.
- Over-precise answers. “O(3n + 2)” should be “O(n)”.
Practise it
After every practice problem, write the complexity before checking the solution, then compare. Within a couple of weeks it becomes automatic. For a more formal definition, Wikipedia’s article on big O notation covers the maths.
Complexity comes up in every coding round; see how to pass a coding interview, our LeetCode study plan and data structures to know for interviews. Our software engineer and Java developer question guides show how it’s asked.
Practise explaining complexity, free. Ghostpilot’s mock interviews ask technical questions for your role and score how clearly you explain your reasoning.
3 mock interviews a month are free, with no card needed.
Start freeQuestions
What is big O notation in simple terms?
It's a way to describe how the running time or memory of an algorithm grows as the input gets bigger. O(n) means it grows in proportion to the input; O(n squared) means doubling the input roughly quadruples the work.
Do interviewers ask about big O?
Almost always. After you solve a coding problem, expect to state its time and space complexity, and often to discuss whether it can be improved.
What's the difference between time and space complexity?
Time complexity is how the number of steps grows; space complexity is how much extra memory grows. A hash map often trades extra space for less time.
Does big O ignore constants?
Yes. O(2n) is written as O(n), and smaller terms are dropped, so O(n squared + n) becomes O(n squared). In interviews, you can mention when constants matter in practice.
Get live help in your next interview
Ghostpilot hears each question and shows an answer from your CV, in a small window above your call. Free to start: 2 live interviews and 3 mock interviews a month, no card needed.
Keep reading



