ProgrammingVery hardGoldman Sachs OA · Amazon 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 / 3)
f(2 * n / 3)
End Procedure- A. O(n)
- B. O(n log n)
- C. O(n²)
- D. O(n^1.58)
Correct answer: B. O(n log n)
T(n) = T(n/3) + T(2n/3) + n. Every level of the recursion tree does at most n work in total, and the longest branch (shrinking by 2/3) has about log₃/₂ n levels, so T(n) = O(n log n).
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? (All variables are integers, so / is integer division) Integer n = 7086351, s = 0, d…
- What will be the output of the following pseudocode? Procedure upd(Integer arr[], Integer n) // arrays are passed by reference arr[0] =…
- What will be the output of the following pseudocode? (& is bitwise AND) Integer x = 255, y = 256, c1 = 0, c2 = 0 while (x > 0) x = x & (x…
- What will be the output of the following pseudocode? Integer cnt = 0 Procedure go(Integer i, Integer s) if (s == 10) cnt = cnt + 1 return…
- What will be the output of the following pseudocode? Function G(Integer n) if (n == 0) return 0 end if return n - G(G(n - 1)) End Function…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code; 'A' to 'Z' have consecutive codes) String w…