Member-only story
Set Zeros in Matrix (Arrays) (coding interview)β Grasp the Pattern! π
This is an interesting array-based problem, and it has been asked by Facebook, Microsoft, Amazon, Oracle, Goldmann Sachs and Apple.
In an interview, it is very important to start your solution from brute force and improve. Interviewers know that their asked problem or similar problem is available on the internet and you might have gone through it.
So relax, read every section carefully and try to answer in the same order unless asked differently (for example, π interviewer might be in a hurry, and they will ask you to give a quick answer).
This problem is part of the 30 days preparation plan for coding interviews.
Table of Contents:
- Problem Description
- Brute Force Solution (extra space)
- Code
- Time & Space Complexity (Brute force)
- Efficient Solution (constant space)
- Code
- Time & Space complexity
Description
Given a 2D matrix, if any cell contains 0, set 0 to that whole row and column. So if cell (i, j) contains 0, then set ith row and jth column to 0.
Example:
Input: 1 1 1 1β¦