DSA FUNDAMENTALS · 9 MIN
Big-O Notation
Big-O describes how the work grows as the input size n grows.
BYTE explains
- O(1): constant — array index lookup.
- O(n): linear — scanning every element once.
- O(log n): halving the problem each step. O(n²): a loop inside a loop over the same data.
- We drop constants and keep the fastest-growing term: 3n² + 5n → O(n²).
Real world: Finding a name in a phone book by flipping page by page (O(n)) vs. opening in the middle each time (O(log n)).
Code
def has_pair(nums, target):
seen = set()
for x in nums: # O(n)
if target - x in seen: # O(1) average
return True
seen.add(x)
return FalseTry it in the online compiler →Checkpoint quiz
Simplify 4n + n² + 7.