A Big O cheat sheet tells you how fast your code slows down as the input grows. O(1) stays flat, O(log n) grows very slowly, O(n) grows in line with the input, O(n log n) is typical for good sorting, and O(n²) or O(2ⁿ) become too slow quickly. This guide has a table and Python code you can run.
HelloEngineers is a community for engineering students in India. This article is written for students who prepare for campus placements and off-campus coding rounds.
Key takeaways
- Big O describes how the work grows when the input size (n) grows. It is not a stopwatch time.
- Learn six classes: O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ).
- Count loops. One loop over n items is O(n). A loop inside a loop is usually O(n²).
- A set or dictionary often turns an O(n²) idea into O(n).
- In interviews, say your complexity out loud, for time and for extra space.
What is Big O notation in simple words?
Big O notation is a short way to say how the number of steps in your code grows with input size n. It ignores small details like constants. It looks at the worst case unless you say otherwise.
Example: if your code does 3n + 5 steps, we write O(n). When n is huge, the 3 and the 5 hardly matter.
What does the Big O cheat sheet look like?
Use this table as a quick reference. The examples use Python 3.
| Big O | Name | Typical example | Feels like |
|---|---|---|---|
| O(1) | Constant | Read arr[i], dictionary lookup | Always fast |
| O(log n) | Logarithmic | Binary search on a sorted list | Very fast, even for huge n |
| O(n) | Linear | One loop, finding the max | Fine for most inputs |
| O(n log n) | Linearithmic | sorted(), merge sort | Good for sorting |
| O(n²) | Quadratic | Two nested loops over n | Slow when n is large |
| O(2ⁿ) | Exponential | Naive recursive Fibonacci | Unusable beyond small n |
Rule of thumb: the further down the table, the sooner your program becomes too slow.
How do you find the time complexity of your code?
Follow these steps for any function.
Step 1: Decide what n is
n is usually the length of the list, the string, or the number in the input. Say it clearly: "n is the length of the array."
Step 2: Count the loops
One loop over n items gives O(n). A loop inside a loop, both over n, gives O(n²). Two loops one after the other give O(n + n), which is still O(n).
Step 3: Look for halving
If the input is cut in half every step, the cost is O(log n). Binary search is the classic case.
Step 4: Drop constants and smaller terms
O(2n + 10) becomes O(n). O(n² + n) becomes O(n²). Keep only the biggest term.
Step 5: Check hidden costs
A call like sorted(arr) inside a loop adds O(n log n) each time. Slicing a list or using in on a list also costs O(n). Count these too.
What does O(1) and O(n) look like in Python?
def first_item(arr):
return arr[0] # O(1): one step, no matter how long arr is
def find_max(arr):
best = arr[0]
for x in arr: # O(n): one pass
if x > best:
best = x
return best
print(first_item([7, 3, 9])) print(find_max([7, 3, 9]))
Expected output:
7 9
`first_item` does the same work for 3 items or 3 million. `find_max` must look at every item once.
## How does O(n²) become O(n) with a set?
Task: check if a list has a duplicate. The slow way compares every pair.
```python
def has_duplicate_slow(arr):
n = len(arr)
for i in range(n):
for j in range(i + 1, n): # nested loops: O(n^2)
if arr[i] == arr[j]:
return True
return False
def has_duplicate_fast(arr):
seen = set()
for x in arr: # one loop: O(n)
if x in seen: # set lookup: O(1) on average
return True
seen.add(x)
return False
data = [4, 8, 15, 16, 23, 8]
print(has_duplicate_slow(data))
print(has_duplicate_fast(data))
Expected output:
True
True
Both are correct. The second one trades extra memory, O(n) space for the set, for much less time. This trade-off is a favourite interview talking point. The same idea powers the Two Sum problem, covered in our coding interview patterns cheat sheet.
How does binary search reach O(log n)?
Binary search needs a sorted list. It checks the middle and throws away half of the rest each time.
def binary_search(arr, target):
lo, hi = 0, len(arr) - 1
steps = 0
while lo <= hi:
steps += 1
mid = (lo + hi) // 2
if arr[mid] == target:
return mid, steps
if arr[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1, steps
arr = list(range(1_000_000)) # 1 million sorted numbers
index, steps = binary_search(arr, 999_999)
print(index)
print(steps <= 20)
Expected output:
999999
True
A linear scan could need up to 1,000,000 checks. Binary search needs at most about 20, because 2²⁰ is just over a million. That is the power of O(log n).
Why is naive Fibonacci O(2ⁿ) and how do you fix it?
The naive version calls itself twice, so the number of calls roughly doubles with each step. It recomputes the same values again and again.
from functools import lru_cache
calls = 0
def fib_slow(n):
global calls
calls += 1
if n < 2:
return n
return fib_slow(n - 1) + fib_slow(n - 2)
print(fib_slow(30))
print(calls)
calls = 0
@lru_cache(maxsize=None)
def fib_fast(n):
global calls
calls += 1
if n < 2:
return n
return fib_fast(n - 1) + fib_fast(n - 2)
print(fib_fast(30))
print(calls)
Expected output:
832040
2692537
832040
31
The slow version makes about 2.7 million calls for n = 30. With caching (memoization) it does the real work only 31 times, so it is O(n). If recursion still feels confusing, read recursion explained with real examples.
What is the complexity of common Python operations?
These are the usual costs in CPython, the standard Python. Treat them as a guide.
| Operation | Typical cost |
|---|---|
list[i] | O(1) |
list.append(x) | O(1) on average |
x in list | O(n) |
list.insert(0, x) | O(n) |
x in set or x in dict | O(1) on average |
sorted(list) | O(n log n) |
len(list) | O(1) |
The most common surprise: x in list looks small but scans the whole list.
How should you explain complexity in an interview?
Use this three-line script after you write your solution:
- "Let n be the length of the input."
- "Time is O(n) because I make one pass and each set lookup is O(1) on average."
- "Space is O(n) because the set can hold up to n items."
Then mention a trade-off: "A brute-force version is O(n²) time but O(1) extra space." Interviewers like this because it shows you understand choices, not just answers. For a bigger practice list, try the top 100 coding interview questions with solutions.
Common mistakes
- Saying "it runs in 2 seconds" instead of a Big O. Speed depends on the machine. Big O is about growth.
- Forgetting hidden loops.
inon a list,sum(),max(), slicing andsorted()all cost time. - Ignoring space. A fast solution that copies the whole array may use too much memory. Always state both.
- Mixing up worst and average case. Set lookups are O(1) on average, not guaranteed in every case. Say "on average".
- Keeping constants. Write O(n), not O(3n).
- Memorising without testing. Run small code and count steps, as in the Fibonacci example.
Best practices
- Start with a brute-force idea, state its complexity, then improve it.
- Ask about input size. If n is at most a few hundred, O(n²) may be fine.
- Practise on 2-3 problems a day and write the complexity next to each solution.
- Learn data structures in order. Our DSA roadmap for beginners shows a sensible sequence, and the Python roadmap covers the language basics first.
FAQ
Do I need Big O for campus placements?
Most coding rounds and technical interviews ask for it, directly or indirectly. Even when nobody asks, a solution that is too slow fails hidden test cases. Knowing Big O helps you pick the right approach before you write code, so it is worth learning early.
What is the difference between time and space complexity?
Time complexity is how the number of steps grows with input size. Space complexity is how the extra memory grows. A solution can be fast but use a lot of memory, like the set-based duplicate check. Interviewers often want you to name both.
Is O(n log n) better than O(n²)?
Yes, for large inputs. For small n the difference is tiny, but as n grows, n² becomes far bigger than n log n. That is why good sorting algorithms like merge sort beat bubble sort, which is O(n²), on big data.
Is Big O the same as the worst case?
Not exactly, but in interviews Big O usually means the worst case. Some algorithms have different best, average and worst cases. Quick sort is fast on average but slow in its worst case. When it matters, say which case you mean.
How do I get better at finding complexity?
Practise on small functions. Write the code, count the loops, and state the complexity. Then add a counter like the examples above and check your guess. After 15-20 problems the pattern becomes automatic and you will not need to think much.
Does Big O change across programming languages?
The growth rate of an algorithm stays the same in C++, Java or Python. Only the constant speed changes. A binary search is O(log n) in all of them. Details of built-in operations can differ, so check the docs of your language.
Next step on HelloEngineers
Pick one problem today, write the brute-force and the improved solution, and note the Big O of both. Share your approach or ask doubts in the HelloEngineers discussions, and keep an eye on jobs and internships to put your skills to use.





