Analyze Time and Auxiliary Space Growth
Core idea
Big-O describes an asymptotic upper bound as input size n grows; it does not report elapsed seconds. Count dominant operations under a stated cost model, and distinguish auxiliary space from input and output storage. For fixed-size numbers and constant-time indexing, one traversal takes O(n) time.
for n in (3, 6): single = sum(1 for _ in range(n)) pairs = sum(1 for _ in range(n) for _ in range(n)) print(n, single, pairs)Doubling n doubles single visits but quadruples pair visits. The two counts grow as n and n squared.
Compare extra storage
values = [2, 4, 6]total = 0for x in values: total += xcopy = [x for x in values]print(total, len(copy))The accumulator needs a fixed number of working values; the copied list allocates n slots.
Common mistake
Big-O is not a benchmark, nor does every nested loop imply quadratic work. Python integer arithmetic may cost more as integers grow; our examples assume bounded-size values.
Practice
Count visits for n=4 and n=8 in one loop and two full nested loops. Identify auxiliary storage separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Growth and storage
Check your understanding of time and space complexity.
What is printed?
print(sum(1 for i in range(5) for j in range(5)))What is printed?
a=[1,2,3,4]b=a[:]print(len(b))What does O(n) describe?
Which extra storage is O(1) under a bounded-value model?