Skip to question
SystemVerilogDesignVerificationFirmwareArchitectureASIC Interview Questions→
/Interview questions/Transpose a square matrix in place

Q187·Free·SystemVerilog

Transpose a square matrix in place

Difficulty
Easy
Topic
Matrices
Language
SV
Interview prompt

Question

Transpose an N x N matrix in place.

Symbolic four-by-four matrix with its main diagonal and three mirrored off-diagonal cell pairs
An in-place transpose swaps each mirrored off-diagonal pair once while leaving the diagonal unchanged.
Candidate starting point

Implementation scaffold

typedef int int_matrix_t[][];

function automatic void transpose_square(ref int_matrix_t matrix);
  // TODO: implement.
endfunction
Reviewed 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

  1. Assume the input has N rows and every row has exactly N elements.
  2. Swap only one side of the diagonal.
  3. Leave diagonal elements unchanged.
  4. Use O(1) extra space.
Exact question handoffPractice Q187

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessEach time you open this Solution, one Practice Credit is used; it is not permanently unlocked. Premium Solution content also uses one credit per opening.
Continue learning

Firmware Guide

Review algorithms, data structures, fixed-memory reasoning, concurrency, and silicon bring-up.

  • Matrices
  • Matrix
  • In place
Firmware Guide →
Continue practicing

Related questions

Q980 · MatricesTranspose a rectangular matrixEasyP→Q258 · MatricesRotate a matrix 90 degrees clockwiseMediumP→Q578 · MatricesTraverse a matrix in spiral orderMediumP→
ASIC.FYI · Learn silicon end to end.info@asic.fyi