ProgrammingVery hardGoldman Sachs OA · TCS Digital
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 g(Integer n)
if (n <= 1) return
for (each i from 1 to n) work() // O(1)
g(n / 2)
g(n / 2)
g(n / 2)
g(n / 2)
End Procedure- A. O(n log n)
- B. O(n³)
- C. O(n²)
- D. O(n^4)
Correct answer: C. O(n²)
T(n) = 4T(n/2) + n. Level k of the recursion has 4ᵏ calls each doing n/2ᵏ work, i.e. 2ᵏ n in total; the last level dominates with about n² work (log₂ 4 = 2 > 1). So O(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? Integer m[3][3] = {{11, 2, 4}, {4, 5, 6}, {10, 8, -12}} Integer d1 = 0, d2 = 0 for…
- What will be the output of the following pseudocode? (/ truncates toward zero and mod keeps the sign of the dividend (as in C)) Integer x…
- What will be the output of the following pseudocode? Boolean seen[0..6] = all false Integer c = 0 for (each i from 1 to 20) Integer r = (i…
- What will be the output of the following pseudocode? Function gcd(Integer a, Integer b) if (b == 0) return a return gcd(b, a mod b) End…
- What will be the output of the following pseudocode? String s = "hello world of code" Integer changes = 0 for (each i from 0 to length(s)…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "ABCABCAB" Integer n = length(s), p = n for…