Big-O and how to approach a problem
Estimate how fast a solution grows, and follow a repeatable method from clarifying questions to testing.
22 min
A coding interview does not only ask whether your code works. It asks whether it keeps working when the input is 1,000 times bigger, and whether you can explain your thinking while you build it. Two skills cover most of that: Big-O and a method.
Big-O describes how the work grows as the input size n grows. It ignores constants and small terms, because for large n only the shape matters. Counting is easier than it sounds:
- A loop over
nitems doing constant work is O(n). - A loop inside a loop, both over
n, is O(n²). - Halving the problem each step (binary search) is O(log n).
- Sorting is O(n log n).
- Looking up a key in a dict or set is O(1) on average.
Space is counted the same way: extra memory your code uses, not counting the input itself.
| Name | n = 1,000 | n = 1,000,000 | Typical example |
|---|---|---|---|
| O(1) | 1 | 1 | dict lookup |
| O(log n) | ~10 | ~20 | binary search |
| O(n) | 1,000 | 1,000,000 | one pass |
| O(n log n) | ~10,000 | ~20,000,000 | sorting |
| O(n²) | 1,000,000 | 10¹² (too slow) | nested loops |
| O(2ⁿ) | more than the atoms in the universe | impossible | trying every subset |
A rough rule: a computer does about 10⁸ simple steps per second in Python, so n = 100,000 is fine for O(n log n) but hopeless for O(n²).
- 1.Clarify. Repeat the problem in your own words. Ask about input size, duplicates, negatives, empty input, sorted or not, and what to return when there is no answer.
- 2.Examples. Make two small examples, including an edge case, and work them by hand.
- 3.Brute force. Say the simple, slow solution out loud and its cost. This proves you understand the problem and gives you a fallback.
- 4.Improve. Ask what repeated work you can remove, or which data structure removes it (a set, a dict, sorting, two pointers).
- 5.Code the improved version, narrating as you type.
- 6.Test by tracing your example and the edge cases, then state time and space complexity.
What common Python operations cost
lst.append(x), lst.pop()
- Cost
- O(1)
- Note
- amortized: occasional resize
lst.insert(0, x), lst.pop(0)
- Cost
- O(n)
- Note
- everything shifts: use
collections.deque
x in lst
- Cost
- O(n)
- Note
- scans the list
x in set, d[key]
- Cost
- O(1)
- Note
- average, thanks to hashing
lst.sort(), sorted(lst)
- Cost
- O(n log n)
- Note
lst[i]
- Cost
- O(1)
- Note
s += "x" in a loop
- Cost
- O(n²) in theory
- Note
- prefer
"".join(parts)
slicing lst[a:b]
- Cost
- O(b - a)
- Note
- copies the piece
Drag n from 1,000 up to 1,000,000. An O(n²) solution goes from instant to many hours. Then ask: for the same n, an O(n) solution would take n divided by the speed, which is a tiny fraction of a second. That gap is what interviewers are listening for.
Seconds for an O(n²) solution
—
The interviewer says: "Given a list of customer ids from today's orders, tell me which id appears twice first." You have just heard the problem.
What do you do first?
Write first_repeat(ids): scan the list from left to right and return the first value you meet that you have already seen. Return -1 if no value repeats. It must run in O(n).
first_repeat([4, 7, 2, 7, 4]) is 7 (the second 7 arrives before the second 4).
Your code must satisfy
- first_repeat([4, 7, 2, 7, 4])→7basic
- first_repeat([1, 2, 3])→-1none
- first_repeat([])→-1empty
- first_repeat([5, 5])→5immediate
- first_repeat([1, 2, 3, 4, 2])→2late repeat
- first_repeat([9])→-1single
def first_repeat(ids):
pass“My code is correct, so the interview is going well.”
In school, a passing answer is the goal.
What is actually true
An interviewer scores communication, problem solving, correctness and complexity together. A correct solution delivered in silence, with no tests and no complexity, can score lower than a clear approach with one small bug you found yourself.
How to catch yourself
Narrate: "I am choosing a set because I need constant-time lookups."
- Big-O describes how work grows with input size: O(1), O(log n), O(n), O(n log n), O(n²).
- For n near 10⁵ or more, O(n²) is too slow; look for a set, a dict, sorting or two pointers.
- Follow the method: clarify, examples, brute force, improve, code, test, state complexity.
- Think out loud: the process is part of the answer.
Complexity checks.
What is the time complexity of a single loop over n items?
- AO(1)
- BO(log n)
- CO(n)
- DO(n²)
Which is O(n²)?
- AA loop over a list
- BA loop inside a loop, both over the list
- CA dict lookup
- DBinary search
Looking up x in some_set costs, on average...
- AO(1)
- BO(n)
- CO(n log n)
- DO(n²)
Why say the brute-force solution aloud first?
- AIt is required
- BIt shows you understand the problem and gives a baseline to improve on
- CTo waste time
- DIt lowers your score
Put the interview method in order.
- AState the brute force
- BImprove it
- CCode it
- DTest and give the complexity
- EClarify the problem
- FWork small examples
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of Coding Interview Prep: Data Structures and Algorithms is planned around your week. Free.