Coding questions · Capital One
Apply commands to transform a matrix
This question evaluates array and matrix manipulation skills, specifically in-place row reversal, row swapping, and 90-degree rotation, and falls under the Coding & Algorithms domain with emphasis on practical implementation.
You are given an integer matrix A of size n × m and a list of string commands to apply in order. Implement the commands and return the final matrix.
Commands
Each command is one of:
-
"reverseRow r"ris a 0-based row index.- Reverse the elements in row
rin-place.
-
"swap r1 r2"r1andr2are 0-based row indices.- Swap the two rows.
-
"rotate"- Rotate the entire matrix 90 degrees clockwise.
- Note that after rotation, the matrix dimensions become
m × n.
Input/Output
- Input:
Aandcommands. - Output: the matrix after applying all commands in order.
Assumptions / Constraints
0 ≤ r, r1, r2 < current_number_of_rowsat the time the command is executed.- Matrix elements fit in 32-bit signed integers.
n, m ≥ 1.
Example
If A = [[1,2,3],[4,5,6]] and commands = ["reverseRow 0", "rotate"], then:
- After
reverseRow 0:[[3,2,1],[4,5,6]] - After
rotate:[[4,3],[5,2],[6,1]]Return[[4,3],[5,2],[6,1]].
Apply Matrix Transform Commands
Hints
- A clockwise rotation is the transpose of the reversed row order.