Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Problem: Matrix chain multiplication: the dimensions of three matrices are given by the array p = [10, 30, 5, 60], meaning A1 is 10×30, A2 is 30×5, A3 is 5×60. What is the minimum number of scalar multiplications needed to compute A1·A2·A3?
- A. 27000
- B. 1500
- C. 9000
- D. 4500
Correct answer: D. 4500
(A1·A2)·A3 costs 10·30·5 + 10·5·60 = 1500 + 3000 = 4500, while A1·(A2·A3) costs 30·5·60 + 10·30·60 = 27000. Counting only the first product gives 1500, and 9000 is only the cost of A2·A3.
More Coding Interview Problems questions
- Problem: Maximum profit with at most two buy-sell transactions (one stock, you must sell before buying again) for the daily prices [3, 3…
- 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…