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]))- A. 49
- B. 48
- C. 56
- 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
- What is printed? python def longest(s): last = {} l = best = 0 for r, ch in enumerate(s): if ch in last and last[ch] >= l: l = last[ch] +…
- What is printed? python import bisect a = sorted([5, 1, 9, 5, 3, 7, 5]) lo = bisect.bisectleft(a, 3) hi = bisect.bisectright(a, 7)…
- A Python program must answer 10^6 membership queries (x in container) against a container with 10^6 items. Which statement is correct?
- What is printed? python def ways(coins, amt): dp = [0] (amt + 1) dp[0] = 1 for c in coins: for v in range(c, amt + 1): dp[v] += dp[v - c]…
- What is printed? python def lcs(a, b): dp = [[0] (len(b) + 1) for in range(len(a) + 1)] for i in range(1, len(a) + 1): for j in range(1…
- What is printed? python items = [(60, 10), (100, 20), (120, 30)] cap = 50 best = 0 w = 0 for v, wt in sorted(items, key=lambda x: x[0] /…