ProgrammingHardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer cat[0..6]
cat[0] = 1
for (each i from 1 to 6)
    cat[i] = 0
    for (each j from 0 to i - 1)
        cat[i] = cat[i] + cat[j] * cat[i - 1 - j]
    end for
end for
Print cat[6]
  1. A. 429
  2. B. 42
  3. C. 720
  4. D. 132

Correct answer: D. 132

The recurrence builds the Catalan numbers 1, 1, 2, 5, 14, 42, 132, so cat[6] = 132. 42 is cat[5] and 429 is cat[7].

More Pseudocode and Code Tracing questions

Exams that ask this topic