Skip to the solution
EasyDSA7 min read

What is Big-O notation?

893 views

The problem

Question presented to candidate: "Explain Big-O notation to me as if I had never heard of it. Then look at a few loops written in Python and tell me their Big-O, and explain why we are allowed to drop constants."

What a strong answer should cover:

  • Big-O describes how running time (or memory) grows as the input size n grows; it is about the shape of the growth, not seconds.
  • Drop constant factors and lower-order terms: 3n² + 5n + 20 is O(n²); 2n is O(n).
  • The common classes in order, with an example each: O(1) index, O(log n) binary search, O(n) one pass, O(n log n) good sorting, O(n²) all pairs, O(2ⁿ) all subsets.
  • Reading code: sequential steps add, nested loops multiply, halving gives log n; Big-O is an upper bound and usually quoted for the worst case (Theta and Omega for tight and lower bounds).
  • Python-specific hidden costs: x in list is O(n) while x in set and dict lookups are O(1) on average; list.pop(0) and slicing are O(n); sort() is O(n log n).

Code / implementation expected: Optional — a few Python functions whose loops the candidate labels O(1), O(log n), O(n), O(n²).

  • complexity
  • big-o

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read · 13 sections

Python code

Edit the code and run it here. Each printed line with an expected value becomes a test.

PythonCount the steps of O(1), O(log n), O(n), O(n log n) and O(n^2) code
PythonLinear vs binary search: comparisons as n grows

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 9 of 154 in DSA

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All DSA questions
Up nextEasy

How do you find the intersection node of two linked lists?

Continue