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)
  1. A. 8 10
  2. B. 128 10
  3. C. 22 10
  4. 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).

More Pseudocode and Code Tracing questions

Exams that ask this topic