Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon OA
Problem: Maximum profit with at most two buy-sell transactions (one stock, you must sell before buying again) for the daily prices [3, 3, 5, 0, 0, 3, 1, 4]. What is the maximum profit?
- A. 6
- B. 4
- C. 8
- D. 2
Correct answer: A. 6
Split the days: best single trade in [3, 3, 5, 0, 0, 3] is 3 (buy 0, sell 3) and in [1, 4] is 3, total 6. One transaction allows only 0 → 4 = 4, unlimited transactions give 2 + 3 + 3 = 8, and stopping after the first rise 3 → 5 gives 2.
More Coding Interview Problems questions
- Problem: How many pairs i < j in the array [2, 8, 9, 18, 4, 32, 50, 72] have a product a[i] × a[j] that is a perfect square?
- Problem: How many three-digit numbers (100 to 999) have strictly increasing digits from left to right (for example 147 qualifies, 151 does…
- Array a = [1, 2, 3, 4, -1, 5, 0, 2]. How many contiguous subarrays have a sum of exactly 10?
- Array a = [2, -2, 3, -3, 4, -4, 1]. How many DISTINCT values appear among the prefix sums P[0], P[1], ..., P[7] (including P[0] = 0)?
- Sorted arrays A = [3, 8, 12, 17, 25] and B = [5, 9, 14, 20]. Choose one element from A and one from B so that the sum is as large as…
- Balloons span closed integer intervals [1,4], [2,3], [4,6], [5,8], [7,9]. One arrow shot at integer x bursts every balloon whose interval…