ProgrammingHardAmazon OA · Goldman Sachs OA
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?
Procedure f(Integer n)
if (n <= 1)
return
end if
for (each i from 1 to n)
work() // O(1)
end for
f(n / 2)
f(n / 2)
End Procedure- A. O(n)
- B. O(n²)
- C. O(n log n)
- D. O(log n)
Correct answer: C. O(n log n)
T(n) = 2T(n/2) + n. Each level of the recursion does n units of work in total, and there are log₂ n levels, so O(n log n) — the same shape as merge sort.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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)…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What will be the output of the following pseudocode? (OR stops evaluating once the left side is true; ++c adds 1 to c and then gives the…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer nxt[8] = {1, 2, 3, 4, 5, 6, 7, 3} Integer slow =…
- What will be the output of the following pseudocode? (mod is applied to (i j) before adding) Integer x = 0 for (each i from 1 to 5) for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer A[2][3] = {{1, 2, 3}, {4, 5, 6}} Integer Bm[3][2]…