ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[5] = {2, 3, -2, 4, -1}
Integer mx = a[0], mn = a[0], best = a[0], t
for (each i from 1 to 4)
if (a[i] < 0)
t = mx
mx = mn
mn = t
end if
mx = max(a[i], mx * a[i])
mn = min(a[i], mn * a[i])
best = max(best, mx)
end for
Print best- A. 6
- B. 24
- C. 48
- D. 4
Correct answer: C. 48
This tracks the largest and smallest products ending at each index, swapping them at a negative number. The two negatives cancel, so the whole array is best: 2 × 3 × (−2) × 4 × (−1) = 48. Stopping at the first negative gives 6.
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[7] = {-7, 1, 5, 2, -4, 3, 0} Integer total = 0…
- What will be the output of the following pseudocode? Integer a[5] = {1, 5, 3, 4, 2}, k = 2, c = 0 for (each i from 0 to 4) for (each j…
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) for (each j from 1 to i i) if (j mod…
- What is the extra space (memory) used by the following recursive pseudocode as a function of n, counting the call stack? Function…
- What will be the output of the following pseudocode? Integer pas[0..6][0..6] = all 0 for (each i from 0 to 6) pas[i][0] = 1 for (each j…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer a[8] = {4, 9, 1, 7, 3…