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]- A. 10
- B. 15
- C. 12
- 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.
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? (All variables are integers, so / is integer division) Integer n = 50, lo = 0, hi =…
- What will be the output of the following pseudocode? (operands are evaluated left to right; arrays are indexed from 0) Integer q[5], front…
- What will be the output of the following pseudocode? String in = "A+BC-D/E", out = "" Stack st = empty // prec: and / are 2, + and - are 1…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; heap[k / 2] is the parent of…
- What will be the output of the following pseudocode? Integer table[7] = all -1, probes = 0, h for each key in {50, 700, 76, 85, 92, 73…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i < n; i = i 2) for (j = 0; j < i; j = j + 1)…