logo

AQA A-Level Computer Science practice

Derive the time complexity of this Python 3 algorithm: for i in range(n): for j in range(i): print(i, j) Give a Big-O classification and justify your answer.

Try the question first, then check the answer and practise the same topic again.

At a glance

Question

Type

practice

Style

Topic

Classification of algorithms

Exam-style question

Try this first

Derive the time complexity of this Python 3 algorithm: for i in range(n): for j in range(i): print(i, j) Give a Big-O classification and justify your answer.

Answer and explanation

Concise answer

The time complexity is O(n^2).

Why this is correct

Although the inner loop does not run exactly n times for every outer-loop iteration, its total number of executions is a triangular sum. That sum grows quadratically, so the algorithm is classified as O(n^2), rather than O(n).

Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.

Common mistake

No common mistake is linked to this question yet.

Related flashcards

No flashcards are published for this page yet.

Related practice questions