Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Zero matrix rows and columns in place

Hardware interview practice

Zero matrix rows and columns in place

MediumFirmware & ValidationC++

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.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
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

  1. Treat m as row-major. Decisions must be based only on zeros present when the function is called.
  2. Use O(1) auxiliary storage beyond scalar variables; the matrix itself may hold row and column markers.
  3. If rows=0 or cols=0, return MZ_OK without dereferencing m; otherwise m must be nonnull and rows*cols must fit size_t.
  4. On a bad argument return MZ_BAD_ARG before writing; on success return MZ_OK after completing all required zeros.
Continue practicing

Related questions

Firmware & ValidationCount errors in a build report→Firmware & ValidationBounded byte-string palindrome check→Firmware & ValidationBounded MMIO command driver→
asic.fyi · Learn silicon end to end.info@asic.fyi