Programming6 min read10 interview questions
Big-O notation without the maths
How to talk about whether code will still be fast with a million items.
In one sentenceBig-O describes how the time or memory a program needs grows as the input grows.
Think of it like thisIt is the difference between finding a name in a sorted phone book by flipping to the middle, and reading every name from page one.
The big picture
Tap any step to see where to use it, how to use it and an example result.
The common shapes
O(1) is constant: the same effort however much data. O(log n) grows very slowly, like halving a list each step. O(n) grows in step with the data. O(n log n) is typical for good sorting. O(n squared) grows fast, and appears when you loop inside a loop over the same data.
- O(1): looking up by key
- O(log n): binary search
- O(n): scanning a list
- O(n squared): comparing every pair
Why it matters
For 100 items nothing matters. For a million, a loop inside a loop is a trillion steps and the program stops being usable, while an O(n) approach is instant.
How to use it
Look for loops, especially nested ones, and for operations that hide a loop, like searching a list. Often a dictionary or set turns a slow search into a quick lookup.
Common mistakes
- Optimising code that handles a handful of items.
- Ignoring hidden loops inside library calls.
- Treating Big-O as the whole story. Real speed also depends on the machine and the data.
Quick recap
- Big-O is about growth, not exact time
- Nested loops over the same data are usually O(n squared)
- Dictionaries give fast lookups
- Measure before optimising
Try it yourself
Write a function that checks for duplicates with two nested loops, then rewrite it with a set. Time both on 10,000 items.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is Big-O notation?Basic
A way to describe how the running time or memory of an algorithm grows as the input gets larger, ignoring small details and constant factors.
What does O(1) mean?Basic
Constant time: the work does not grow with input size, like reading an item from an array by index.
What does O(n) mean?Basic
Linear time: the work grows in direct proportion to the input, like scanning a list once to find a value.
What does O(n²) mean, and when does it appear?Basic
Work grows with the square of the input, usually from a loop inside a loop over the same data. Doubling the input roughly quadruples the time.
What is O(log n) and where have you seen it?Intermediate
Time grows very slowly because each step halves the problem, as in binary search on a sorted list. A million items take about twenty steps.
What is O(n log n)?Intermediate
The cost of efficient sorting algorithms such as merge sort. It is a bit slower than linear but far better than quadratic.
Why do we drop constants and smaller terms?Intermediate
Because for large inputs the fastest-growing term dominates, so O(2n + 10) is simply O(n). Big-O describes growth, not exact timing.
What is the difference between time and space complexity?Intermediate
Time complexity is how long the work takes as input grows. Space complexity is how much extra memory it needs.
What are best, average and worst case?Advanced
They describe different inputs. Quick sort is O(n log n) on average but O(n²) in the worst case. Big-O is often quoted for the worst case unless stated otherwise.
How would you improve an O(n²) solution?Advanced
Look for repeated work: use a hash map or set for fast lookups, sort first and then use two pointers or binary search, or cache results. Often this trades a little memory for much less time.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.