ProgrammingHardCapgemini · Infosys pattern

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

Correct answer: C. O(n^2)

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

More Pseudocode and Code Tracing questions

Exams that ask this topic