ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer p[6] = {7, 1, 5, 3, 6, 4}
Integer lo = p[0], one = 0, many = 0
for (each i from 1 to 5)
lo = min(lo, p[i])
one = max(one, p[i] - lo)
if (p[i] > p[i - 1])
many = many + p[i] - p[i - 1]
end if
end for
Print one, many- A. 6 7
- B. 5 7
- C. 5 5
- D. 4 7
Correct answer: B. 5 7
one is the best single buy-then-sell profit: buy at 1, sell at 6, profit 5. many adds every day-to-day rise, which is the best profit with unlimited trades: (5 − 1) + (6 − 3) = 7. Output 5 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? (Arrays are indexed from 0) Integer a[5] = {2, 3, 1, 1, 4} Integer jumps = 0, curEnd…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer h[9] = {1, 8, 6, 2, 5, 4, 8, 3, 7} Integer i = 0…
- What will be the output of the following pseudocode? (>> is right shift, & is AND) Integer a[3] = {4, 14, 2}, total = 0 for (each b from 0…
- What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts) Integer x = 1, r = 0 for (each i from 1 to…
- What will be the output of the following pseudocode? Integer m[3][3] = {{2, 0, 1}, {1, 3, 2}, {1, 1, 1}} Integer det = m[0][0] (m[1][1]…
- What will be the output of the following pseudocode? Integer m[4][4] = {{4, 3, 2, -1}, {3, 2, 1, -1}, {1, 1, -1, -2}, {-1, -1, -2, -3}}…