Module 2 · Measuring Speed and Memory · Lesson 4 of 40

Big-O shortcuts

Drop constants, keep the biggest term, add steps in a row, multiply loops inside loops, and spot log n.

In this lesson · about 20 minutes

  • Simplify expressions like 3n + 5 to O(n)
  • Add complexities for code in sequence and multiply for nested loops
  • Recognise O(log n) when a value is halved or doubled

You do not need to count every single step. A few shortcuts give you the Big-O quickly.

Rule 1: drop constants

3n + 5 steps is just O(n). When n is huge, the 3 and the 5 don't change the shape of the growth. Two loops one after the other (2n) is also O(n).

Rule 2: keep only the biggest term

n² + n + 100 is O(n²). When n is a million, n² is a trillion, and the other parts are tiny next to it.

Rule 3: steps in a row add up

One loop over n items, then another loop over m items: O(n + m). If both are the same list, that is O(2n), which is O(n).

Rule 4: loops inside loops multiply

A loop of n with a loop of m inside: O(n × m). Three nested loops over n: O(n³).

Rule 5: halving means log n

If each step cuts the problem in half (or doubles a counter until it reaches n), the number of steps is about log₂ n. For n = 1,000,000 that is only about 20. We just write O(log n), because the base of the log doesn't matter for Big-O.

i = 1
while i < n:
    i = i * 2     # 1, 2, 4, 8, 16, ...   → O(log n)

Rule 6: if / else, take the slower branch

If one branch is O(1) and the other is O(n), the worst case is O(n).

Watch out: hidden loops

Some single lines hide a loop. x in my_list, my_list.index(x), min(nums) and sorted(nums) all look at many items. A line like if x in my_list: inside a loop makes the whole thing O(n²).

Try it: halving is fast

Count how many times you can halve a number before it reaches 1. Try n = 1,000,000,000.

Exercise

Count the doublings

Write doublings(n): start with i = 1 and keep doubling i while i < n. Return how many times you doubled. (This is the O(log n) loop from the lesson.)

Sorting drill

Name that Big-O

Put each piece of code under its time complexity (n is the length of nums).

  • return nums[-1]
  • for x in nums: total += x
  • for a in nums: for b in nums: ...
  • while n > 1: n = n // 2
  • for x in nums: if x in nums: ...
  • len(nums)
  • Two loops over nums, one after the other
  • Binary search in a sorted list

0 of 8 sorted

Check your understanding0 of 4 correct

Question 1

1.Simplify 5n² + 3n + 20.

Question 2

2.A loop over list A (n items) and then a loop over list B (m items). What is it?

Question 3

3.About how many halvings take 1,000,000 down to 1?

Question 4

4.Why is 'if x in my_list' inside a for loop over my_list slow?

Quiz score: 0 of 4

Exercises solved: 0 of 1

Tip: answer every quiz question correctly first.

Noviq Academy is free and for learning. This course follows the topics of a set of DSA study notes, rewritten in Noviq's own words with Python examples. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.