ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c[0..5] = {1, 0, 0, 0, 0, 0}
for (each i from 1 to 5)
    for (each j from 1 to i)
        c[j] = c[j] + c[j - 1]
    end for
end for
Print c[2]
  1. A. 10
  2. B. 6
  3. C. 21
  4. D. 14

Correct answer: D. 14

Going forward, c[j] picks up the already-updated c[j − 1], so on pass i, c[1] becomes i and c[2] grows by that new c[1]: c[2] = 0, 2, 5, 9, 14. The usual right-to-left loop would give C(5, 2) = 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic