ProgrammingVery hardCapgemini · Infosys pattern

What is the tightest time complexity of the following pseudocode, in terms of n? (Treat any constant like 2 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 2 as a fixed constant, not growing with n.)
for (each i from 1 to n)
    for (each j from 1 to n)
        for (each p from 1 to n)
            Print i, j, p
        end for
    end for
end for
Print "done 2"
  1. A. O(n^2)
  2. B. O(log n)
  3. C. O(n log n)
  4. D. O(n^3)

Correct answer: D. O(n^3)

Counting total iterations as a function of n (constants such as 2 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^3) for this structure.

More Pseudocode and Code Tracing questions

Exams that ask this topic