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?

  1. A. 27000
  2. B. 1500
  3. C. 9000
  4. 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

Exams that ask this topic