CodeBlanks Logo Icon CodeBlanks

Time complexity

Learn how runtime grows with input size.

Time complexity

An algorithm's time complexity describes how its running time grows as the input size increases. Larger input means longer runtime.

For example, scanning an array of n elements takes n steps; doubling the input doubles the time.

Growth rate

Different algorithms have different growth rates. One may halve the input each step, another may visit every element.

The growth rate determines the difference on large inputs. A lower growth rate means a more efficient algorithm overall.

Practice Array sum

Write array_sum(arr). Sum elements and return. Print 10 with print(array_sum([1, 2, 3, 4])).

Starter code

solution.py
def array_sum(arr):
    # Sum elements

Sample solution

solution.py
def array_sum(arr):
    # Sum elements
    total = 0
    for x in arr:
        total = total + x
    return total
print(array_sum([1, 2, 3, 4]))

Expected output

10

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does time complexity measure?

  • Runtime growth Correct answer
  • Memory usage
  • Lines of code
  • Compile time

Explanation: Time complexity measures runtime growth.

2 Growth rate

Question
A lower growth rate means a more ____ algorithm.
Answer
A lower growth rate means a more efficient algorithm.

Explanation: efficient is right.

3 Time complexity: match the terms

Term Meaning
Time complexity Runtime growth
Input size n
Growth rate Efficiency
Big-O Notation

All lessons in Beginner