Algorithms and Data Structures · Junior · Question 65 of 75
What is the complexity of this loop?
for (let i = 1; i < n; i *= 2) {
console.log(i);
}- 1O(n)
- ✓Correct answer: O(log n)
- 3O(n²)
Explanation
i doubles each step: 1, 2, 4, 8... It reaches n after about log₂ n iterations. Loops where the variable is multiplied or divided by a constant are logarithmic.