ProgrammingVery hardAccenture · Cognizant pattern

What is the tightest time complexity of the following pseudocode, in terms of n? (Treat any constant like 3 as a fixed constant, not growing with n.)

What is the tightest time complexity of the following pseudocode, in terms of n? (Treat any constant like 3 as a fixed constant, not growing with n.)
Integer i = n
while (i > 1)
    for (each k from 1 to 3*n)
        Print k
    end for
    i = i / 2
end while
  1. A. O(n log n)
  2. B. O(1)
  3. C. O(n)
  4. D. O(log n)

Correct answer: A. O(n log n)

Counting total iterations as a function of n (constants such as 3 do not change growth order; nested loops that each run ~n times multiply, and a loop that doubles/halves contributes a log n factor) gives O(n log n) for this structure.

More Pseudocode and Code Tracing questions

Exams that ask this topic