ProgrammingVery hardAmazon OA · Flipkart coding round

What is printed?

What is printed?
def most_water(h):
    i, j, best = 0, len(h) - 1, 0
    while i < j:
        best = max(best, min(h[i], h[j]) * (j - i))
        if h[i] < h[j]:
            i += 1
        else:
            j -= 1
    return best
print(most_water([1, 8, 6, 2, 5, 4, 8, 3, 7]))
  1. A. 49
  2. B. 48
  3. C. 56
  4. D. 42

Correct answer: A. 49

Two pointers always move the shorter wall inward, because the area is limited by the shorter side. The best pair is heights 8 and 7 with distance 7, giving area 49.

More Python Programming questions

Exams that ask this topic