What is matrix exponentiation and when is it used?
Why Interviewers Ask This
This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.
Answer
Matrix exponentiation computes Mⁿ (matrix raised to power n) in O(k³ log n) time using repeated squaring (binary exponentiation), where k is the matrix dimension. Binary exponentiation: Mⁿ = M^(n/2) × M^(n/2) if n even; M × M^(n-1) if n odd. Divide n by 2 each step → O(log n) matrix multiplications, each O(k³). Applications: (1) Fibonacci in O(log n): [F(n+1)] = [1 1]^n × [F(1)]. The transformation matrix [[1,1],[1,0]] raised to the n-th power gives Fibonacci numbers. Without this: O(n) DP; with matrix exponentiation: O(log n) — critical for very large n; (2) Linear recurrences in O(log n): any linear recurrence relation (T(n) = aT(n-1) + bT(n-2) + cT(n-3)) can be expressed as matrix multiplication and computed in O(k³ log n); (3) Path counting in graphs: Aⁿ[i][j] = number of paths of length n from vertex i to j; (4) Dynamic programming on graphs with exactly k steps; (5) Number of strings of length n matching a regular expression (via DFA matrix). Matrix exponentiation turns O(n) recurrences into O(log n), which is significant when n can be 10^18 (common in competitive programming) — the difference between an answer in 1 second vs 10^13 seconds.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Data Structures & Algorithms codebase.