ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function rob(Integer a[], Integer lo, Integer hi)
Integer inc = 0, exc = 0, t
for (each i from lo to hi)
t = max(inc, exc)
inc = exc + a[i]
exc = t
end for
return max(inc, exc)
End Function
Integer h[5] = {2, 7, 9, 3, 1}
Print rob(h, 0, 4), max(rob(h, 0, 3), rob(h, 1, 4))- A. 12 12
- B. 11 11
- C. 12 10
- D. 12 11
Correct answer: D. 12 11
In a row, the best non-adjacent pick is 2 + 9 + 1 = 12. If the houses form a circle, the first and last are neighbours, so solve without the last (best 2 + 9 = 11) and without the first (best 7 + 3 = 10, or 9 + 1 = 10): 11. Output 12 11.
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 cost[3] = {10, 15, 20} Integer a = 0, b = 0, c // cheapest cost to stand on…
- What will be the output of the following pseudocode? Pair iv[11] = {(1,4), (3,5), (0,6), (5,7), (3,9), (5,9), (6,10), (8,11), (8,12)…
- What will be the output of the following pseudocode? Integer arr[6] = {900, 940, 950, 1100, 1500, 1800} Integer dep[6] = {910, 1200, 1120…
- What will be the output of the following pseudocode? (& is AND, >> is right shift) Integer x = 40, pos = 1 while ((x & 1) == 0) x = x >> 1…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 10, steps = 0…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; and / are evaluated left to…