PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates dynamic programming, algorithmic analysis, and sensitivity reasoning for matrix-chain multiplication, and it belongs to the Coding & Algorithms domain; it is commonly asked because it tests understanding of optimal substructure, trade-offs in parenthesization, and correctness versus greedy heuristics.

  • hard
  • Apple
  • Coding & Algorithms
  • Data Scientist

Compute optimal matrix-chain multiplication order

Company: Apple

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Matrix Chain Multiplication optimal parenthesization. Given matrices with dimensions: A1 (10×30), A2 (30×5), A3 (5×60), A4 (60×2), A5 (2×100). (a) Use dynamic programming to compute the minimum scalar multiplications and the optimal parenthesization; show your DP tables m[i,j] and s[i,j] and the final order. (b) Prove optimal substructure and explain why a greedy choice on local smallest dimension fails. (c) Analyze time and space complexity; discuss when Strassen‑like algorithms could reduce cost in practice given these skinny/fat shapes. (d) If A3 is 5×k and A4 is k×2 with k unknown at design time, derive the threshold on k where the optimal parenthesization changes.

Quick Answer: This question evaluates dynamic programming, algorithmic analysis, and sensitivity reasoning for matrix-chain multiplication, and it belongs to the Coding & Algorithms domain; it is commonly asked because it tests understanding of optimal substructure, trade-offs in parenthesization, and correctness versus greedy heuristics.

You are given a chain of matrices A1, A2, ..., An that must be multiplied together. Because matrix multiplication is associative, you may choose any valid parenthesization, and different parenthesizations can require vastly different amounts of work. The dimensions of the matrices are encoded in an array `p` of length `n + 1`, where matrix `Ai` has dimensions `p[i-1] x p[i]` (using 1-based matrix indices). Multiplying a `(p x q)` matrix by a `(q x r)` matrix costs `p * q * r` scalar multiplications and yields a `(p x r)` matrix. Using dynamic programming, compute and return the **minimum total number of scalar multiplications** needed to compute the product `A1 * A2 * ... * An` over all possible parenthesizations. Example: For `p = [10, 30, 5, 60, 2, 100]` (matrices A1(10x30), A2(30x5), A3(5x60), A4(60x2), A5(2x100)), the optimal order `((A1 * (A2 * (A3 * A4))) * A5)` costs 600 + 300 + 600 + 2000 = 3500 scalar multiplications, which is the minimum, so the answer is 3500. Edge cases: if there are fewer than two matrices (i.e. `len(p) <= 2`), no multiplication is needed and the answer is 0.

Constraints

  • 2 <= len(p) <= 1000 (so 1 <= n <= 999 matrices); for len(p) <= 2 the answer is 0
  • 1 <= p[i] (each dimension is a positive integer)
  • The product can grow large; use a 64-bit integer accumulator in languages with fixed-width ints (e.g. C++/Java).

Examples

Input: ([10, 30, 5, 60, 2, 100],)

Expected Output: 3500

Explanation: The original Apple instance. Optimal order ((A1*(A2*(A3*A4)))*A5) costs 600+300+600+2000 = 3500.

Input: ([5, 10, 3, 12, 5],)

Expected Output: 405

Explanation: A1(5x10),A2(10x3),A3(3x12),A4(12x5). Optimal is (A1*A2)*(A3*A4): 150 + 180 + 75 = 405, which beats A1*(A2*(A3*A4)) at 580.

Input: ([40, 20, 30, 10, 30],)

Expected Output: 26000

Explanation: Classic CLRS-style instance; the DP yields 26000 as the minimum scalar multiplications.

Input: ([10, 20, 30],)

Expected Output: 6000

Explanation: Only one way to multiply two matrices A1(10x20),A2(20x30): cost 10*20*30 = 6000.

Input: ([10, 20],)

Expected Output: 0

Explanation: A single matrix (n=1): nothing to multiply, so the cost is 0.

Input: ([5],)

Expected Output: 0

Explanation: Degenerate input with no matrix (n=0): answer is 0.

Input: ([10, 30, 5, 60],)

Expected Output: 4500

Explanation: Prefix sub-instance A1(10x30),A2(30x5),A3(5x60). Optimal (A1*A2)*A3: 1500 + 10*5*60 = 1500+3000 = 4500.

Hints

  1. Let m[i][j] be the minimum cost to multiply matrices Ai..Aj. Then m[i][i] = 0.
  2. For i < j, try every split point k in [i, j-1]: m[i][j] = min over k of m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]. The last term is the cost of multiplying the two resulting (p[i-1] x p[k]) and (p[k] x p[j]) blocks.
  3. Fill the table by increasing chain length so that all smaller subchains are already solved before you need them.
  4. Greedy (always multiply the locally cheapest adjacent pair) does NOT work — a locally cheap product can leave an awkward shape that inflates later costs.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimum Cells to Bridge a Magic Grid - Apple (hard)
  • Find Common Prefix Across Strings - Apple (easy)
  • Find Minimum Processing Rate - Apple
  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)