Magicsheet logo

Modify the Matrix

Easy
100%
Updated 6/1/2025

Asked by 1 Company

Modify the Matrix

What is this problem about?

The Modify the Matrix problem gives you a matrix where some entries contain -1. Replace each -1 with the maximum value in its column. This Modify the Matrix coding problem tests clean two-pass matrix traversal: first compute column maximums, then replace -1 entries.

Why is this asked in interviews?

Fidelity asks this easy-level problem to test array and matrix traversal skills under time pressure. It validates that candidates can perform a two-pass matrix operation correctly: gathering column-wise information in the first pass and applying it in the second. The array and matrix interview pattern is cleanly demonstrated.

Algorithmic pattern used

Two-pass column scan. In the first pass, find the maximum value in each column (ignoring -1 values). In the second pass, replace any -1 entry with the precomputed maximum for that column.

Example explanation

Matrix:

1  -1  3
4   5  6
-1  2  9

Column maximums (ignoring -1): col 0 = max(1,4) = 4, col 1 = max(5,2) = 5, col 2 = max(3,6,9) = 9. After replacement:

1  5  3
4  5  6
4  2  9

Common mistakes candidates make

  • Including -1 values when computing the column maximum.
  • Single-pass approach (can't replace -1 while computing max simultaneously).
  • Row-major order confusion — iterating correctly for column operations.
  • Off-by-one in column index tracking.

Interview preparation tip

Any matrix problem where "cells depend on their column's aggregate value" requires a two-pass approach. This is a clean example of the precompute-then-apply pattern. Internalize this structure: first pass gathers metadata (column max, row sum, etc.); second pass applies changes. This pattern extends to harder problems like "matrix with special values" and "set zeros in matrix."

Similar Questions