ProgrammingVery hardAmazon OA · Goldman Sachs OA

What will be the output of the following pseudocode? (Arrays are indexed from 0)

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, j = 8, best = 0
while (i < j)
    best = max(best, (j - i) * min(h[i], h[j]))
    if (h[i] < h[j])
        i = i + 1
    else
        j = j - 1
    end if
end while
Print best
  1. A. 56
  2. B. 40
  3. C. 49
  4. D. 64

Correct answer: C. 49

Two pointers move the shorter wall inward. The best container uses the walls of height 8 (index 1) and 7 (index 8): width 7 × height 7 = 49.

More Pseudocode and Code Tracing questions

Exams that ask this topic