Exam-style question
Try this first
Derive the time complexity of this Python 3 algorithm: count = 0 for i in range(n): count = count + 1 Explain how the number of loop iterations changes as n increases.
Model answer
What a good answer should say
- The time complexity is O(n).
- The loop runs once for each value in the input range, so it performs n iterations.
- The assignment and addition inside the loop each take constant time.
- Therefore, the total running time grows linearly as n increases.
Explanation
Why this works
To derive the complexity, identify the repeated operation and count how often it occurs. The loop body is executed n times, giving a linear relationship between input size and running time.
Constant factors are ignored in Big-O notation, so the result is O(n).
Common mistake
No common mistake is linked to this question yet.
