← Time & Space Complexity Quiz: all 8 questions
Time & Space Complexity Quiz · Question 3 of 8
What is the time complexity of this loop? (code)
What is the time complexity of this loop?
i = n
while i > 1:
i = i // 2
- A.O(n)
- B.O(log n)
- C.O(√n)
- D.O(1)
Answer & explanation
Correct answer: O(log n)
The value halves every step, so it takes about log₂(n) steps to reach 1.
Can you get the rest right without peeking?
Take the timed Time & Space Complexity Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →