Data Structures & AlgorithmsHardProduct Company SDE · two pointers
Problem: Water is poured into a container of heights [1, 8, 6, 2, 5, 4, 8, 3, 7]. Choose two lines to maximise the area of water they hold, using the two-pointer method. What is printed?
Problem: Water is poured into a container of heights [1, 8, 6, 2, 5, 4, 8, 3, 7]. Choose two lines to maximise the area of water they hold, using the two-pointer method. What is printed?
h = [1, 8, 6, 2, 5, 4, 8, 3, 7]
i, j = 0, len(h) - 1
best = 0
while i < j:
best = max(best, min(h[i], h[j]) * (j - i))
if h[i] < h[j]:
i += 1
else:
j -= 1
print(best)What is printed?- A. 48
- B. 49
- C. 56
- D. 42
Correct answer: B. 49
The pair of heights 8 (index 1) and 7 (index 8) gives 7 * 7 = 49. Always moving the taller pointer skips this pair, so the shorter pointer must move.
More Coding Interview Problems questions
- Problem: Trucks carry boxes: each truck type is (count, units per box), and at most 10 boxes fit on the truck. Maximise units per truck…
- Problem: How many contiguous subarrays of [1, 2, 3, 4, 5] have sum exactly 5? Run the code. python a = [1, 2, 3, 4, 5] print(sum(1 for i…
- Problem: What is the length of the longest zigzag subsequence (values alternate strictly up and down) of [1, 7, 4, 9, 2, 5]? Run the DP…
- Problem: Two piles of stones (values [1, 6, 11, 5]) are to be divided into two groups whose sums differ as little as possible. What is the…
- Problem: You have 2 eggs and a building with 10 floors. What is the minimum number of drops needed in the worst case to find the highest…
- Problem: How many distinct ways can the string 'leetcode' be segmented into words from the dictionary {'leet', 'code', 'leetcode', 'lee'…