Skip to main content

56. Set Matrix Zeroes

mediumAsked at Salesforce

Set entire rows and columns to zero where a zero exists in the matrix. Salesforce uses this to test in-place algorithms with O(1) space.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses in-place matrix transforms in their dashboard rendering.
  • LeetCode Discuss (2025-09)Tests the 'use first row/col as markers' trick.

Problem

Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's. You must do it in place.

Constraints

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -2^31 <= matrix[i][j] <= 2^31 - 1

Examples

Example 1

Input
matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output
[[1,0,1],[0,0,0],[1,0,1]]

Example 2

Input
matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output
[[0,0,0,0],[0,4,5,0],[0,3,1,0]]

Approaches

1. Set sets for rows/cols

Track which rows and cols contain a zero; then zero them out.

Time
O(m*n)
Space
O(m+n)
function setZeroes(matrix) {
  const rows = new Set(), cols = new Set();
  for (let i = 0; i < matrix.length; i++) for (let j = 0; j < matrix[0].length; j++) {
    if (matrix[i][j] === 0) { rows.add(i); cols.add(j); }
  }
  for (let i = 0; i < matrix.length; i++) for (let j = 0; j < matrix[0].length; j++) {
    if (rows.has(i) || cols.has(j)) matrix[i][j] = 0;
  }
}

Tradeoff: O(m+n) space. Salesforce will push for O(1).

2. Use first row/col as markers

Track if first row/col itself has a zero. Use cells in first row/col to mark zero status of remaining rows/cols. Apply, then zero first row/col if needed.

Time
O(m*n)
Space
O(1)
function setZeroes(matrix) {
  const m = matrix.length, n = matrix[0].length;
  let firstRowZero = false, firstColZero = false;
  for (let j = 0; j < n; j++) if (matrix[0][j] === 0) firstRowZero = true;
  for (let i = 0; i < m; i++) if (matrix[i][0] === 0) firstColZero = true;
  for (let i = 1; i < m; i++) for (let j = 1; j < n; j++) {
    if (matrix[i][j] === 0) { matrix[i][0] = 0; matrix[0][j] = 0; }
  }
  for (let i = 1; i < m; i++) for (let j = 1; j < n; j++) {
    if (matrix[i][0] === 0 || matrix[0][j] === 0) matrix[i][j] = 0;
  }
  if (firstRowZero) for (let j = 0; j < n; j++) matrix[0][j] = 0;
  if (firstColZero) for (let i = 0; i < m; i++) matrix[i][0] = 0;
}

Tradeoff: O(1) extra space. The first row/col double as 'zero markers' for interior rows/cols. Salesforce's expected answer.

Salesforce-specific tips

Salesforce specifically tests this 'use the matrix as scratch space' trick. Bonus signal: explain WHY you need separate firstRowZero / firstColZero booleans — the (0,0) cell can't simultaneously mark its row and column zero, since reading and writing collide.

Common mistakes

  • Trying to use (0,0) as both row and col marker — single cell can't track two facts.
  • Forgetting to handle the first row/col at the end — its cells were used as markers, must be explicitly zeroed if needed.
  • Iterating starting at (0,0) in the propagation loop — corrupts markers.

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • Game of Life (LC 289) — similar in-place transformation.
  • Spiral Matrix (LC 54).
  • Set rows/cols to a value other than zero (less ambiguous markers).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why separate booleans for first row/col?

If matrix[0][j] is zero, it could mean (a) (0,j) was originally zero, or (b) some interior (i,j) was zero. The boolean lets us distinguish these cases for the first row/col.

Could I use a different sentinel?

Yes if the range is bounded (e.g., use Infinity for 'will be set to zero'). The first-row-as-marker trick is more elegant when input range is unbounded.

Practice these live with InterviewChamp.AI

Drill Set Matrix Zeroes and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →