FastPrepApply Matrix Operations
Problems/Apply Matrix Operations

Apply Matrix Operations

Capital One logoCapital One● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Capital One backend intern online assessment in Toronto had a medium question about changing a 2D array. The report lists three kinds of change: flipping rows or columns, a 90-degree turn to the left, and trading rows or columns with each other.

The report does not give the input format, how the changes are written, or any limits. Here, the changes come as a list of short commands applied in order. Indexes start at 0 and always refer to the matrix as it is at that moment. Reversing a row or column flips the order of its values, and swapping trades two whole rows or two whole columns. The limits are our choice.

Our version is about 70% the same as the reported question.

You are given an m x n integer matrix matrix and a list of strings operations. Apply the operations one by one, in order, and return the final matrix. Rows and columns are numbered from 0.

Each operation is one of these:

  • "reverseRow r": reverse the order of the values in row r.
  • "reverseColumn c": reverse the order of the values in column c, so its top value goes to the bottom.
  • "swapRows a b": swap row a and row b.
  • "swapColumns a b": swap column a and column b.
  • "rotateCounterclockwise": turn the whole matrix 90 degrees counterclockwise. The last column becomes the first row, so a matrix with r rows and c columns becomes one with c rows and r columns.

An index always refers to the matrix as it is when that operation runs. After a turn, the number of rows and the number of columns may have swapped. In a swap, a and b may be equal, and then nothing changes.

Function

applyOperations(matrix: int[][], operations: String[]) → int[][]

Examples

Example 1

matrix = [[1,2,3],[4,5,6]]operations = ["reverseRow 0","swapColumns 0 2","rotateCounterclockwise"]return = [[3,4],[2,5],[1,6]]
matrix
01201231456
→
return
01034125216

Reversing row 0 gives [[3,2,1],[4,5,6]]. Swapping columns 0 and 2 gives [[1,2,3],[6,5,4]]. Turning it 90 degrees counterclockwise makes the last column [3,4] the first row, so the 2 x 3 matrix becomes the 3 x 2 matrix [[3,4],[2,5],[1,6]].

Example 2

matrix = [[1,2],[3,4],[5,6]]operations = ["rotateCounterclockwise","reverseColumn 2","swapRows 0 1"]return = [[1,3,6],[2,4,5]]
matrix
01012134256
→
return
01201361245

The turn changes the 3 x 2 matrix into the 2 x 3 matrix [[2,4,6],[1,3,5]]. Column 2 exists only after the turn. Reversing it swaps 6 and 5, giving [[2,4,5],[1,3,6]]. Swapping rows 0 and 1 gives [[1,3,6],[2,4,5]].

Example 3

matrix = [[5,-1],[0,8]]operations = ["rotateCounterclockwise","rotateCounterclockwise","swapRows 1 1"]return = [[8,0],[-1,5]]
matrix
0105-1108
→
return
010801-15
Moved or new

Two turns of 90 degrees turn the matrix upside down: [[8,0],[-1,5]]. Swapping row 1 with itself changes nothing.

Constraints

  • 1 <= m, n <= 50, where m is the number of rows and n is the number of columns of matrix
  • -1000 <= matrix[i][j] <= 1000
  • 1 <= operations.length <= 1000
  • Every row of matrix has the same length.
  • Each operation has exactly one of the five forms above. Words and numbers are separated by single spaces, and numbers have no leading zeros.
  • Every index in an operation is valid for the matrix as it is when that operation runs.

More Capital One Online Assessment questions

  • Rank Football TeamsOA · Seen Oct 2026
  • Count Subarrays Matching a Comparison PatternOA · Seen Oct 2026
  • Arithmetic Triplets After QueriesOA · Seen Oct 2026
  • Branch with the Most FilesOA · Seen Oct 2026
  • Count One-Swap Number PairsOA · Seen Oct 2026
  • Distinct Values on Maximum-Sum FramesOA · Seen Oct 2026
  • Earliest Meeting Time for All EmployeesOA · Seen Oct 2026
  • Earn the Most 🐝OA · Seen Oct 2026
  • Sort Adjacent PairsOA · Seen Oct 2026
  • Bouncing Diagonal WeightsOA · Seen Oct 2026

Similar questions at other companies

  • Maximum Diamond SumHudson River Trading · OAHudson River Trading interview questions
  • Robot Navigation Around LasersRoblox · OARoblox interview questions
  • Apply a Reversi MoveAmazon · OA
  • Flip, Invert, and Smooth a Binary ImageAbridge · OAAbridge interview questions
  • First Completed Mingo LineEpic · OAEpic interview questions
  • Rotate a Square Matrix ClockwiseVisa · OAVisa interview questions
  • Sort Concentric Matrix Borders ClockwiseByteDance · OAByteDance interview questions
  • Sparse Valid Conv2D From ScratchNVIDIA · OANVIDIA interview questions
  • Rotate the Box and Drop the StonesZipRecruiter · OAZipRecruiter interview questions
  • 2048 Move SimulatorBenchling · OABenchling interview questions

Capital One interview experiences

  • Capital One Experienced Engineer Power Day and OA: 6 Experiences
  • Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
  • Capital One TDP Agentic Round (AI Coding): 7 Experiences
  • Capital One SWE Power Day, Level Not Reported: 6 Experiences
  • Capital One TIP Intern OA and Power Day: 13 Experiences
  • Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)
All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public int[][] applyOperations(int[][] matrix, String[] operations) {
        // Write your code here.
    }
}
Checking account…