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- A. O(n log n)
- B. O(1)
- C. O(n)
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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 = 81, y = 10 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…
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 4. What does each of the 4 calls print, in…
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n, Integer…