ProgrammingVery hardProduct company OA · code-tracing level
What is the time complexity of the following pseudocode, as a function of n?
What is the time complexity of the following pseudocode, as a function of n?
for (i = 1; i * i <= n; i = i + 1)
for (j = 1; j <= n; j = j * 2)
Print "*"
end for
end for- A. O(n log n)
- B. O(√n)
- C. O(log² n)
- D. O(√n · log n)
Correct answer: D. O(√n · log n)
The outer loop runs while i² ≤ n, i.e. about √n times. The inner loop doubles j from 1 to n, about log₂ n + 1 times, independent of i. Total O(√n · log n). O(n log n) misreads the outer bound as i ≤ n; O(√n) ignores the inner loop.
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 indexed from 0; / is integer division; 'from … to …' includes both ends.)…
- What will be the output of the following pseudocode? Integer c = 0, d, k for (each x from 1 to 30) k = 0 for (each d from 1 to x) if (x…
- What will be the output of the following pseudocode? (Array positions run from 1 to 10.) Integer alive[1..10] = all 1, left = 10, pos = 0…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, << and >> are shifts.) Integer x = 22, r = 0 for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; / is integer division.) Integer a[7] = {1, 3, 5, 5, 5, 7…
- What will be the output of the following pseudocode? Integer s = 0 Procedure p(Integer n) if (n == 0) return end if p(n - 1) s = s + n p(n…