ProgrammingVery hardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function p(Integer n, Integer k)
if (n == 0)
return 1
end if
if (n < 0 OR k == 0)
return 0
end if
return p(n, k - 1) + p(n - k, k)
End Function
Print p(8, 8), p(8, 3)- A. 8 10
- B. 128 10
- C. 22 10
- D. 22 8
Correct answer: C. 22 10
p(n, k) counts ways to write n as a sum of parts no bigger than k (order ignored): either no part equals k, or remove one part k. So p(8, 8) is the number of partitions of 8, 22, and p(8, 3) = 10 (partitions of 8 into 1's, 2's and 3's).
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 calls = 0 Function f(Integer a, Integer b) calls = calls + 1 if (b == 0)…
- What will be the output of the following pseudocode? Integer g[5][5] = {{1,1,0,0,0}, {1,0,0,1,1}, {0,0,1,0,0}, {1,0,1,1,0}, {1,0,0,0,1}}…
- What will be the output of the following pseudocode? Integer m[4][4], s = 0 for (each i from 0 to 3) for (each j from 0 to 3) m[i][j] = 4…
- What will be the output of the following pseudocode? Boolean comp[0..30] = all false Integer s = 0 for (each i from 2 to 30) if (not…
- What will be the output of the following pseudocode? Function isPrime(Integer n) if (n < 2) return false for (each d from 2 to n - 1) if…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function phi(Integer n)…