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 h[6] = {2, 1, 5, 6, 2, 3}, best = 0
for (each i from 0 to 5)
    Integer mn = h[i]
    for (each j from i to 5)
        mn = min(mn, h[j])
        best = max(best, mn * (j - i + 1))
    end for
end for
Print best
  1. A. 12
  2. B. 6
  3. C. 10
  4. D. 8

Correct answer: C. 10

The loops find the largest rectangle in the histogram: bars 5 and 6 side by side give height 5 × width 2 = 10. Using all six bars gives only 1 × 6 = 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic