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]- A. 429
- B. 42
- C. 720
- 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].
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer a[6] = {3, 1, 2, 6, 4, 5}, swaps = 0, m, n = 6 for (each i from 0 to n - 2) m…
- What will be the output of the following pseudocode? Integer a[6] = {5, 2, 4, 6, 1, 3}, shifts = 0, key, j for (each i from 1 to 5) key =…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code and char(k) the reverse; 'A'..'Z' have…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 987654321, c = 0…
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 4, 5, 7, 9} Integer i = 0, j = 5, c = 0 while (i < j) if (a[i]…
- What will be the output of the following pseudocode? Integer a[7] = {2, -1, 4, 3, -2, 5, 1} Integer s = a[0] + a[1] + a[2], best = s for…