Q187FreeSystemVerilog
Transpose a square matrix in place
Interview prompt
Question
Transpose an N x N matrix in place.

Candidate starting point
Implementation scaffold
typedef int int_matrix_t[][];
function automatic void transpose_square(ref int_matrix_t matrix);
// TODO: implement.
endfunctionReviewed example
Trace one case
Input
matrix = [[1, 2], [3, 4]]Expected output
[[1, 3], [2, 4]]Swapping the off-diagonal pair performs the square transpose in place.
What to cover
Requirements
- Assume the input has N rows and every row has exactly N elements.
- Swap only one side of the diagonal.
- Leave diagonal elements unchanged.
- Use O(1) extra space.
