cswork / libraryOpen practice workspace

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:

  1. "reverseRow r"

    • r is a 0-based row index.
    • Reverse the elements in row r in-place.
  2. "swap r1 r2"

    • r1 and r2 are 0-based row indices.
    • Swap the two rows.
  3. "rotate"

    • Rotate the entire matrix 90 degrees clockwise.
    • Note that after rotation, the matrix dimensions become m × n.

Input/Output

  • Input: A and commands.
  • Output: the matrix after applying all commands in order.

Assumptions / Constraints

  • 0 ≤ r, r1, r2 < current_number_of_rows at 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

  1. A clockwise rotation is the transpose of the reversed row order.