Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Matrix problems

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Matrix problems

Set Matrix Zeroes

advancedlayer I12 · Matrix problems5 tests

Given an m × n matrix, if any cell holds 0, set that cell's entire row and column to 0. Do it in place.

  • Only the zeros present in the original matrix trigger a wipe — zeros you write must not cascade
  • You must use O(1) extra space: no m×n copy, and no arrays of row/column flags
  • The trick is to store the flags inside the matrix itself
  • The return value is ignored; the caller reads the matrix it passed in

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen