Hardware interview practice
Zero matrix rows and columns in place
Firmware owns a row-major matrix. If an original element is zero, every element in that element's row and column must become zero. Write C++ code for the matrix transformation with constant auxiliary storage.
Starting point
Question code
enum MZStatus { MZ_OK, MZ_BAD_ARG };
MZStatus zero_rows_cols(int32_t *m,
size_t rows, size_t cols);Reviewed example
Work through one case
Input
Matrix [[1,2,0],[4,5,6]] with rows=2 and cols=3.Expected output
Return MZ_OK; matrix becomes [[0,0,0],[4,5,0]].The original zero at row 0, column 2 marks exactly its entire row and its entire column for clearing.
What to cover
Requirements
- Treat m as row-major. Decisions must be based only on zeros present when the function is called.
- Use O(1) auxiliary storage beyond scalar variables; the matrix itself may hold row and column markers.
- If rows=0 or cols=0, return MZ_OK without dereferencing m; otherwise m must be nonnull and rows*cols must fit size_t.
- On a bad argument return MZ_BAD_ARG before writing; on success return MZ_OK after completing all required zeros.
