Apply Matrix Operations
Updated
Problem statement
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 rowr."reverseColumn c": reverse the order of the values in columnc, so its top value goes to the bottom."swapRows a b": swap rowaand rowb."swapColumns a b": swap columnaand columnb."rotateCounterclockwise": turn the whole matrix 90 degrees counterclockwise. The last column becomes the first row, so a matrix withrrows andccolumns becomes one withcrows andrcolumns.
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]]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]]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]]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, wheremis the number of rows andnis the number of columns ofmatrix-1000 <= matrix[i][j] <= 10001 <= operations.length <= 1000- Every row of
matrixhas 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)