Predict the output
Each call counts its inner steps. What does this print?
def steps(n: int) -> tuple[int, int]:
one = sum(1 for _ in range(n))
two = sum(1 for _ in range(n) for _ in range(n))
return one, two
a, b = steps(10)
c, d = steps(20)
print(c // a, d // b)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 2 4
Right: doubling n doubles the linear count and quadruples the quadratic count.
Growth ratios like these are what Big O summarises, which is why constant factors drop out.
Why not the others?
- B 2 2. The nested loop grows with n × n, so doubling n multiplies it by 4.
- C 1 2. Both counts grow when n grows.