ProgrammingHardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer P[4][4] = all 0
Integer A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
for (each i from 1 to 3)
    for (each j from 1 to 3)
        P[i][j] = A[i - 1][j - 1] + P[i - 1][j] + P[i][j - 1] - P[i - 1][j - 1]
    end for
end for
Print P[3][3] - P[1][3] - P[3][1] + P[1][1]
  1. A. 45
  2. B. 33
  3. C. 28
  4. D. 12

Correct answer: C. 28

P is a 2-D prefix-sum table, and the printed expression is the sum of the block of rows 2–3 and columns 2–3 of A: 5 + 6 + 8 + 9 = 28.

More Pseudocode and Code Tracing questions

Exams that ask this topic