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.

Ghostpilot team · · 4 min read

A dark blue card with the title "Big O notation for interviews" next to the Ghostpilot ghost

In short

Practise this free: a mock interview for Coding round (algorithms), scored, with a stronger answer. 3 a month, no card.

Start a mock interview

Big 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:

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:

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

  1. Sequential steps add. A loop, then another loop: O(n + n) = O(n).
  2. Nested loops multiply. A loop inside a loop over the same input: O(n squared).
  3. Halving gives log n. If each step cuts the remaining work in half, it’s O(log n).
  4. 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.
  5. 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:

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:

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

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 free

Questions

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

Data structures to know for coding interviewsTechnical and coding · 4 min read
How to pass a coding interview (a step-by-step plan)Technical and coding · 4 min read
LeetCode study plan for interviews (8 weeks)Technical and coding · 4 min read
Technical interviews for non-technical rolesTechnical and coding · 4 min read