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- A. O(n)
- B. O(n^3)
- C. O(n^2)
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Consider this naive recursive Fibonacci. How many TOTAL calls to fib (including the initial call) happen when computing fib(6)? Function…
- What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array…
- What will be the output of the following pseudocode? (^ is bitwise XOR.) Integer x = 90, y = 76 x = x ^ y y = x ^ y x = x ^ y Print x, y
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 3. What does each of the 6 calls print, in…