Problems/

Analysing Loops and Nested Loops

medium

Concept check — write, then compare

Determine the time complexity of each snippet below, and explain how you reason it out — the method matters more than the answers.

Snippet A

total = 0
for i in range(n):
    for j in range(n):
        total += i * j

Snippet B

total = 0
for i in range(n):
    for j in range(i):
        total += 1

Snippet C

i = 1
while i < n:
    i = i * 2

Snippet D

for i in range(n):        # phase 1
    do_constant_work()
for j in range(m):        # phase 2
    do_constant_work()

Explain the general rules you used (nesting, sequencing, halving/doubling, dependent loop bounds).

Explaining out loud is the test — write as if teaching someone.