FastPrepSet Matrix Zeroes
Problems/Set Matrix Zeroes

Set Matrix Zeroes

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the rule of zeroing rows and columns, an in-place requirement, and a follow-up about processing in blocks for a matrix that cannot fit in memory. It does not give the dimensions, value limits, a function signature, examples, or an interface for external storage. Our version is about 94% the same as the main reported question.

You are given a nonempty rectangular integer matrix matrix. If an element is 0, set every element in its row and column to 0.

Modify the matrix in place using O(1) auxiliary space, then return the same transformed matrix for comparison.

Only zeroes present in the original matrix determine which rows and columns become zero.

Interview follow-up

Be prepared to explain how to process a matrix too large to fit in memory by scanning storage-backed blocks. This version is for discussion only. Your function gets the in-memory matrix described above.

Function

setZeroes(matrix: int[][]) → int[][]

Examples

Example 1

matrix = [[1,1,1],[1,0,1],[1,1,1]]return = [[1,0,1],[0,0,0],[1,0,1]]
matrix
012011111012111
→
return
012010110002101
Moved or new

The original zero at row 1, column 1 makes the middle row and middle column zero.

Example 2

matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]return = [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
matrix
0123001201345221315
→
return
0123000001045020310
Moved or new

The original zeroes in the first row require that row and the first and fourth columns to become zero.

Constraints

  • 1 <= matrix.length <= 200.
  • 1 <= matrix[0].length <= 200.
  • Every row has the same number of columns.
  • -2^31 <= matrix[i][j] <= 2^31 - 1.
  • The algorithm must use O(1) auxiliary space for the in-memory matrix.

More Google Onsite Interview questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Maximal SquareAmazon · ONSITE INTERVIEW
  • Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
  • Rotting OrangesSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
  • Minimum Time to Rot All OrangesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
  • Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
  • Search a 2D MatrixWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions

Interview reports that mention this question

  • Google Intern Prefix Sums and Heaps: 6 Experiences (Part 3 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[][] setZeroes(int[][] matrix) {
      // Write your code here.
    }
}
Checking account…