ProgrammingHardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer t[4][4] = {{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}}     // row r has r + 1 numbers
for (each r from 2 down to 0)
    for (each c from 0 to r)
        t[r][c] = t[r][c] + min(t[r + 1][c], t[r + 1][c + 1])
    end for
end for
Print t[0][0]
  1. A. 21
  2. B. 11
  3. C. 14
  4. D. 10

Correct answer: B. 11

Working upward, each entry becomes the cheapest path from it to the bottom moving to an adjacent number below. The best path is 2 → 3 → 5 → 1 = 11.

More Pseudocode and Code Tracing questions

Exams that ask this topic