Skip to content
Lesson 1 of 10 · Coding Interview Prep: Data Structures and Algorithms

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 n items 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.

Namen = 1,000n = 1,000,000Typical example
O(1)11dict lookup
O(log n)~10~20binary search
O(n)1,0001,000,000one pass
O(n log n)~10,000~20,000,000sorting
O(n²)1,000,00010¹² (too slow)nested loops
O(2ⁿ)more than the atoms in the universeimpossibletrying 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. 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. 2.Examples. Make two small examples, including an edge case, and work them by hand.
  3. 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. 4.Improve. Ask what repeated work you can remove, or which data structure removes it (a set, a dict, sorting, two pointers).
  5. 5.Code the improved version, narrating as you type.
  6. 6.Test by tracing your example and the edge cases, then state time and space complexity.
Reference

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
Try it

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.

100000
100 million steps/s

Seconds for an O(n²) solution

—

Decide

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?

Exercise7 checks
python

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
starter code2 lines
def first_repeat(ids):
    pass
Common misconception

“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.
Check yourself

Complexity checks.

1Multiple choice

What is the time complexity of a single loop over n items?

  • AO(1)
  • BO(log n)
  • CO(n)
  • DO(n²)
2Multiple choice

Which is O(n²)?

  • AA loop over a list
  • BA loop inside a loop, both over the list
  • CA dict lookup
  • DBinary search
3Multiple choice

Looking up x in some_set costs, on average...

  • AO(1)
  • BO(n)
  • CO(n log n)
  • DO(n²)
4Multiple choice

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
5Put in order

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.