FAANG Interview Prep
Foundations, Memory & Complexity
Big-O notation, time/space analysis, memory layoutRecursion Complete Guide
Base cases, call stack, tail recursion, memoizationArrays & Array ADT
Static/dynamic arrays, operations, amortized analysisStrings
Pattern matching, string algorithms, encoding, manipulationMatrices
2D arrays, sparse matrices, matrix operations, traversalsLinked Lists
Singly, doubly, circular lists, pointer manipulationStack
LIFO, push/pop, expression evaluation, backtrackingQueue
FIFO, circular queue, deque, priority queueTrees
Binary trees, traversals, expression trees, threaded treesBST & Balanced Trees
Search, insert, delete, AVL, red-black, B-treesHeaps, Sorting & Hashing
Min/max heaps, heapsort, hash tables, collision handlingGraphs, DP, Greedy & Backtracking
BFS, DFS, shortest paths, dynamic programming, optimizationIntroduction to Matrices
Matrices are two-dimensional arrays that play a crucial role in computer science, mathematics, and engineering. Understanding efficient matrix storage and operations is essential for graphics, machine learning, scientific computing, and solving system of linear equations.
2D Array Representation
In memory, 2D arrays can be stored in two ways: Row-Major Order (C, Python) or Column-Major Order (Fortran, MATLAB).
Special Matrices
Special matrices exploit structural patterns to compress storage from \(O(n^2)\) to \(O(n)\). The key insight: if most elements are zero or can be computed from a formula (e.g., \(M[i][j] = M[j][i]\) for symmetric matrices), store only the unique non-trivial elements and reconstruct the rest on access. This is critical in scientific computing, computer graphics (transformation matrices), and machine learning (covariance matrices, weight sparsity).
Diagonal Matrix
A diagonal matrix has non-zero elements only on the main diagonal (where \(i = j\)). Instead of storing \(n^2\) elements, we only need \(n\) elements. Real-world example: scaling matrices in computer graphics — a 4×4 diagonal matrix scales x, y, z coordinates independently. Multiplication by a diagonal matrix is \(O(n)\) instead of \(O(n^2)\) since each row is just multiplied by the corresponding diagonal element.
Triangular Matrices
Triangular matrices have non-zero elements only above (upper) or below (lower) the diagonal. Storage: \(n(n+1)/2\) elements instead of \(n^2\). Why they matter: Gaussian elimination produces upper triangular form, LU decomposition splits any matrix into lower × upper triangular, and Cholesky decomposition (for symmetric positive definite matrices) uses \(L \cdot L^T\). Solving \(Ax = b\) with a triangular matrix takes only \(O(n^2)\) via back/forward substitution — much faster than the general \(O(n^3)\).
Symmetric Matrix
A symmetric matrix satisfies M[i][j] = M[j][i]. We only need to store the lower (or upper) triangular portion, requiring n(n+1)/2 elements.
Tri-diagonal Matrix
A tri-diagonal matrix has non-zero elements only on the main diagonal and the diagonals immediately above and below it. It requires only 3n-2 elements.
Special Matrix Space Comparison
| Matrix Type | Non-zero Pattern | Space Required | For n=100 |
|---|---|---|---|
| Full Matrix | All elements | n² | 10,000 |
| Diagonal | i = j | n | 100 |
| Triangular | i = j or i = j | n(n+1)/2 | 5,050 |
| Symmetric | M[i][j] = M[j][i] | n(n+1)/2 | 5,050 |
| Tri-diagonal | |i - j| = 1 | 3n - 2 | 298 |
Sparse Matrices
A sparse matrix is one where most elements are zero. When the number of non-zero elements is much smaller than n×m, storing only the non-zero elements saves significant space.
Coordinate Format (COO)
The simplest sparse format stores each non-zero element with its row index, column index, and value.
Compressed Sparse Row (CSR) Format
CSR is the industry standard for sparse matrix storage in scientific computing. It uses three arrays: values (non-zero values in row order), col_indices (column of each value), and row_ptr (where each row starts in the values array). CSR gives \(O(\text{nnz}/n)\) average row access and efficient matrix-vector multiplication. SciPy, TensorFlow, and CUDA all use CSR as their primary sparse format. The tradeoff: column access is slow (\(O(\text{nnz})\)) — use CSC for that.
Compressed Sparse Column (CSC) Format
CSC is the column analog of CSR: values and row_indices are stored in column order, with a col_ptr array indicating where each column starts. CSC excels at column slicing and column-wise operations — making it ideal for solving sparse linear systems (\(Ax = b\)) where column access patterns dominate. MATLAB uses CSC internally for its sparse matrices. In practice, choose CSR for row-heavy operations and CSC for column-heavy ones; converting between them is \(O(\text{nnz})\).
Sparse Format Comparison
| Operation | COO | CSR | CSC |
|---|---|---|---|
| Construction | \(O(1)\) per element | O(nnz) | O(nnz) |
| Random Access | O(nnz) | O(log k) | O(log k) |
| Row Slice | O(nnz) | \(O(k)\) | O(nnz) |
| Column Slice | O(nnz) | O(nnz) | \(O(k)\) |
| Mat-Vec Mult | O(nnz) | O(nnz) ? | O(nnz) |
k = number of non-zeros in the row/column
Polynomial Representation
Polynomials can be represented using arrays. For sparse polynomials (many zero coefficients), we store only non-zero terms.
Sparse Polynomial and Operations
LeetCode Practice Problems
Medium 54. Spiral Matrix
Given an m×n matrix, return all elements in spiral order.
Medium 48. Rotate Image
Rotate the image (n×n matrix) by 90 degrees clockwise in-place.
Medium 73. Set Matrix Zeroes
If an element is 0, set its entire row and column to 0. Do it in-place.
Medium 311. Sparse Matrix Multiplication
Given two sparse matrices, return their multiplication result.
Next in the Series
In Part 6: Linked Lists, we’ll move from contiguous memory to pointer-based structures with linked lists — singly, doubly, and circular variants plus the manipulation techniques interviewers love.