ProgrammingHardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer u[1..10], i2 = 1, i3 = 1, i5 = 1, nx
u[1] = 1
for (each k from 2 to 10)
    nx = min(u[i2] * 2, u[i3] * 3, u[i5] * 5)
    u[k] = nx
    if (nx == u[i2] * 2) i2 = i2 + 1
    if (nx == u[i3] * 3) i3 = i3 + 1
    if (nx == u[i5] * 5) i5 = i5 + 1
end for
Print u[10]
  1. A. 10
  2. B. 15
  3. C. 12
  4. D. 16

Correct answer: C. 12

This generates numbers whose only prime factors are 2, 3, 5 in order, with no repeats (all equal candidates advance): 1, 2, 3, 4, 5, 6, 8, 9, 10, 12. The 10th is 12.

More Pseudocode and Code Tracing questions

Exams that ask this topic