The Find Valid Matrix Given Row and Column Sums interview question is a construction challenge. You are given two arrays representing the desired sums for each row and each column of a matrix. You need to build any matrix of non-negative integers that satisfies these sum requirements. A valid solution is always guaranteed to exist.
Companies like Google and Squarepoint Capital ask the Find Valid Matrix coding problem to test a candidate's understanding of Greedy interview patterns. It evaluations whether you can identify a simple, repetitive strategy to satisfy constraints without needing complex backtracking or linear programming. It's a test of "constructive algorithm" design.
This problem is solved using a Greedy Construction pattern.
(0, 0)).(i, j), assign the maximum possible value that doesn't violate either the current row sum or column sum:
val = min(rowSum[i], colSum[j]).val from both rowSum[i] and colSum[j].rowSum = [3, 8], colSum = [4, 7]
min(3, 4) = 3. rowSum[0] becomes 0, colSum[0] becomes 1.min(0, 7) = 0.min(8, 1) = 1. rowSum[1] becomes 7, colSum[0] becomes 0.min(7, 7) = 7.
Result Matrix:3 0
1 7
In "construction" problems where any valid answer is accepted, always try the most "local" greedy strategy first. If you can satisfy one constraint at a time without breaking others, you've likely found the intended solution.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Max Increase to Keep City Skyline | Medium | Solve | |
| Maximum Matrix Sum | Medium | Solve | |
| Reconstruct a 2-Row Binary Matrix | Medium | Solve | |
| Minimum Operations to Make Columns Strictly Increasing | Easy | Solve | |
| Largest Submatrix With Rearrangements | Medium | Solve |